当前位置: 首页 > news >正文

国内wordpress网站排名优化服务

国内wordpress,网站排名优化服务,深圳app开发公司,杭州市网站建设可重入锁(InterProcessMutex):这种锁允许同一个客户端多次获取同一把锁而不会被阻塞,类似于Java中的ReentrantLock。它通过在Zookeeper的指定路径下创建临时序列节点来实现锁的功能。如果获取锁失败,当前线程会监听前一…

可重入锁(InterProcessMutex):这种锁允许同一个客户端多次获取同一把锁而不会被阻塞,类似于Java中的ReentrantLock。它通过在Zookeeper的指定路径下创建临时序列节点来实现锁的功能。如果获取锁失败,当前线程会监听前一个节点的变动情况并等待,直到被唤醒或超时

package com.zz.lock;import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;public class CuratorReentrantLockExample {private final String lockPath = "/curator/lock"; // 锁的Zookeeper路径private CuratorFramework client; // Curator客户端private InterProcessMutex mutex; // 可重入锁// 初始化Curator客户端和可重入锁public void init() {// 设置Zookeeper服务地址String connectString = "192.168.200.130:2181";// 设置重试策略ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(3000, 3);// 创建Curator客户端client = CuratorFrameworkFactory.newClient(connectString, retryPolicy);client.start();// 创建可重入锁mutex = new InterProcessMutex(client, lockPath);}// 执行业务逻辑,使用可重入锁public void executeBusinessLogic() {try {// 获取锁mutex.acquire();// 模拟业务逻辑System.out.println("当前线程获得锁,开始执行业务逻辑。");// 模拟重入逻辑reentrantLock();// 模拟业务逻辑System.out.println("当前线程完成业务逻辑执行。");} catch (Exception e) {e.printStackTrace();} finally {// 确保释放锁if (mutex.isAcquiredInThisProcess()) {try {mutex.release();} catch (Exception e) {e.printStackTrace();}}}}// 模拟可重入逻辑public void reentrantLock() {try {// 再次获取同一把锁mutex.acquire();System.out.println("当前线程重入成功,再次获得同一把锁。");// 模拟一些操作...} catch (Exception e) {e.printStackTrace();} finally {// 释放锁if (mutex.isAcquiredInThisProcess()) {try {mutex.release();} catch (Exception e) {e.printStackTrace();}}}}// 程序入口public static void main(String[] args) {CuratorReentrantLockExample example = new CuratorReentrantLockExample();example.init();// 执行业务逻辑example.executeBusinessLogic();}
}

不可重入锁(InterProcessSemaphoreMutex):与可重入锁类似,但不允许同一个线程在持有锁的情况下再次获取该锁。这种锁很容易导致死锁,使用时需要特别注意

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;public class CuratorReentrantLockExample {private final String lockPath = "/curator/lock"; // 锁的Zookeeper路径private CuratorFramework client; // Curator客户端private InterProcessMutex mutex; // 可重入锁// 初始化Curator客户端和可重入锁public void init() {// 设置Zookeeper服务地址String connectString = "127.0.0.1:2181";// 设置重试策略ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(3000, 3);// 创建Curator客户端client = CuratorFrameworkFactory.newClient(connectString, retryPolicy);client.start();// 创建可重入锁mutex = new InterProcessMutex(client, lockPath);}// 执行业务逻辑,使用可重入锁public void executeBusinessLogic() {try {// 获取锁mutex.acquire();// 模拟业务逻辑System.out.println("当前线程获得锁,开始执行业务逻辑。");// 模拟重入逻辑reentrantLock();// 模拟业务逻辑System.out.println("当前线程完成业务逻辑执行。");} catch (Exception e) {e.printStackTrace();} finally {// 确保释放锁if (mutex.isAcquiredInThisProcess()) {try {mutex.release();} catch (Exception e) {e.printStackTrace();}}}}// 模拟可重入逻辑public void reentrantLock() {try {// 再次获取同一把锁mutex.acquire();System.out.println("当前线程重入成功,再次获得同一把锁。");// 模拟一些操作...} catch (Exception e) {e.printStackTrace();} finally {// 释放锁if (mutex.isAcquiredInThisProcess()) {try {mutex.release();} catch (Exception e) {e.printStackTrace();}}}}// 程序入口public static void main(String[] args) {CuratorReentrantLockExample example = new CuratorReentrantLockExample();example.init();// 执行业务逻辑example.executeBusinessLogic();}
}

读写锁(InterProcessReadWriteLock):提供一对相关的锁,读锁可以被多个读操作共享,而写锁则独占。一个拥有写锁的线程可以获取读锁,但读锁不能升级为写锁。这种锁是公平的,保证用户按请求顺序获取锁。(读写锁在逻辑上有点像数据库的事务)

package com.zz.lock;import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock;public class CuratorReadWriteLockExample {private final String lockPath = "/curator/read-write-lock"; // 锁的Zookeeper路径private CuratorFramework client; // Curator客户端private InterProcessReadWriteLock lock; // 读写锁// 初始化Curator客户端和读写锁public void init() {// 设置Zookeeper服务地址String connectString = "192.168.200.130:2181";// 设置重试策略ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(3000, 3);// 创建Curator客户端client = CuratorFrameworkFactory.newClient(connectString, retryPolicy);client.start();// 创建读写锁lock = new InterProcessReadWriteLock(client, lockPath);}// 执行读操作public void executeReadOperation() throws Exception {try {// 获取读锁lock.readLock().acquire();// 模拟读操作System.out.println("读操作开始,线程安全地读取数据。");// 模拟读操作延迟Thread.sleep(3000);System.out.println("读操作结束。");} catch (Exception e) {e.printStackTrace();} finally {// 释放读锁if (lock.readLock().isAcquiredInThisProcess()) {lock.readLock().release();}}}// 执行写操作public void executeWriteOperation() throws Exception {try {// 获取写锁lock.writeLock().acquire();// 模拟写操作System.out.println("写操作开始,线程独占资源进行写入。");// 模拟写操作延迟Thread.sleep(3000);System.out.println("写操作结束,更新了数据。");} catch (Exception e) {e.printStackTrace();} finally {// 释放写锁if (lock.writeLock().isAcquiredInThisProcess()) {lock.writeLock().release();}}}// 程序入口public static void main(String[] args) {CuratorReadWriteLockExample example = new CuratorReadWriteLockExample();example.init();// 启动多个读操作线程for (int i = 0; i < 5; i++) {new Thread(() -> {try {example.executeReadOperation();} catch (Exception e) {e.printStackTrace();}}).start();}// 启动写操作线程new Thread(() -> {try {example.executeWriteOperation();} catch (Exception e) {e.printStackTrace();}}).start();}
}

联锁(InterProcessMultiLock):这是一个锁的容器,可以同时获取多个锁。如果获取过程中任何一个锁请求失败,已获取的所有锁都会被释放。这在需要同时持有多个锁执行操作的场景中非常有用

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.framework.recipes.locks.InterProcessLock;
import org.apache.curator.framework.recipes.locks.InterProcessMultiLock;
import java.util.Arrays;
import java.util.List;public class InterProcessMultiLockExample {private CuratorFramework client;private List<String> lockPaths = Arrays.asList("/lock1", "/lock2", "/lock3");private List<InterProcessLock> locks = lockPaths.stream().map(path -> new InterProcessMutex(client, path)).collect(Collectors.toList());private InterProcessMultiLock multiLock;public void init() {String connectString = "127.0.0.1:2181";RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 3);client = CuratorFrameworkFactory.newClient(connectString, retryPolicy);client.start();multiLock = new InterProcessMultiLock(locks);}public void executeProtectedOperation() {try {multiLock.acquire();// 所有锁都已获取,执行你的业务逻辑System.out.println("All locks acquired, performing business logic.");// 业务逻辑...} catch (Exception e) {e.printStackTrace();} finally {// 确保释放所有锁multiLock.release();}}public static void main(String[] args) {InterProcessMultiLockExample example = new InterProcessMultiLockExample();example.init();example.executeProtectedOperation();}
}

信号量(InterProcessSemaphoreV2):Curator提供了一种信号量实现,可以控制同时访问某个资源的线程数量。通过acquire方法请求获取信号量,使用完成后通过returnAll方法释放

信号量(Semaphore)确实可以起到限流的作用。在分布式系统中,信号量是一种常用的限流工具,它通过控制同时访问某个资源或执行某个操作的线程数量来实现限流。以下是信号量实现限流的几个关键点:1. **资源限制**:信号量通过一个计数器来限制可用资源的数量。例如,如果你有10个停车位,你可以设置信号量的初始值为10。2. **请求处理**:当一个线程需要访问资源时,它首先尝试从信号量中获取一个“许可”(lease)。如果信号量的计数器大于0,该线程成功获取一个许可,然后继续执行。否则,线程将被阻塞,直到其他线程释放资源。3. **释放资源**:线程完成资源访问后,必须释放它获取的许可,通过将许可返还给信号量来实现。这会将信号量的计数器增加1,允许其他等待的线程获取许可。4. **公平性**:信号量通常是公平的,意味着线程将按照它们请求许可的顺序来获得它们。这有助于避免某些线程长时间等待访问资源。5. **跨JVM共享**:在分布式系统中,不同的进程可能在不同的JVM中运行。Apache Curator 提供的 `InterProcessSemaphoreV2` 允许跨JVM共享信号量状态,因此所有相关进程都能协调地访问共享资源。6. **自动资源回收**:如果持有信号量许可的线程或进程崩溃,Curator 会自动释放该许可,确保资源不会被永久占用,其他线程可以继续获取该资源。通过这种方式,信号量可以有效地控制对共享资源的并发访问,防止系统过载,从而实现限流。这在许多场景下都非常有用,比如数据库连接池、线程池、外部服务调用等。
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessSemaphoreV2;
import org.apache.curator.framework.recipes.locks.Lease;
import org.apache.curator.retry.ExponentialBackoffRetry;public class InterProcessSemaphoreV2Demo {private static final String PATH = "/semaphore/path";private static CuratorFramework client;public static void main(String[] args) throws Exception {// 初始化Curator客户端client = CuratorFrameworkFactory.newClient("localhost:2181", new ExponentialBackoffRetry(3000, 2));client.start();// 创建InterProcessSemaphoreV2实例,设置最大租约数为5InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, PATH, 5);// 线程示例,模拟同时请求信号量的多个线程for (int i = 0; i < 10; i++) {new Thread(new SemaphoreTask(semaphore)).start();}// 等待一段时间,让线程执行Thread.sleep(10000);// 关闭客户端连接client.close();}static class SemaphoreTask implements Runnable {private final InterProcessSemaphoreV2 semaphore;public SemaphoreTask(InterProcessSemaphoreV2 semaphore) {this.semaphore = semaphore;}@Overridepublic void run() {try {Lease lease = semaphore.acquire();System.out.println(Thread.currentThread().getName() + " acquired a lease.");// 模拟业务逻辑处理Thread.sleep(3000);// 释放信号量租约semaphore.returnLease(lease);System.out.println(Thread.currentThread().getName() + " returned a lease.");} catch (Exception e) {e.printStackTrace();}}}
}

 

分布式锁的实现原理:Curator的分布式锁通常是基于Zookeeper的临时顺序节点来实现的。当多个客户端尝试获取锁时,Zookeeper会为它们创建顺序节点,并让它们按照节点的序号依次尝试获取锁。未获取到锁的客户端会监听前一个序号的节点,一旦前一个节点释放锁,监听的客户端就会尝试获取锁


文章转载自:
http://dinncoantifascist.wbqt.cn
http://dinncoancilla.wbqt.cn
http://dinncomonorhinous.wbqt.cn
http://dinncoupsurge.wbqt.cn
http://dinncomonroeism.wbqt.cn
http://dinncohemimetabolic.wbqt.cn
http://dinncovast.wbqt.cn
http://dinncoscammony.wbqt.cn
http://dinncosynectics.wbqt.cn
http://dinncozamboni.wbqt.cn
http://dinncolonghair.wbqt.cn
http://dinncoknower.wbqt.cn
http://dinnconephalism.wbqt.cn
http://dinncosalvar.wbqt.cn
http://dinncohomomorphic.wbqt.cn
http://dinncohymenopteron.wbqt.cn
http://dinncopenology.wbqt.cn
http://dinncowrestling.wbqt.cn
http://dinncointerbreed.wbqt.cn
http://dinncoscrapbasket.wbqt.cn
http://dinncosupercarrier.wbqt.cn
http://dinncokommandatura.wbqt.cn
http://dinncoastrict.wbqt.cn
http://dinncoassur.wbqt.cn
http://dinncocollarless.wbqt.cn
http://dinncovilely.wbqt.cn
http://dinncoheptachord.wbqt.cn
http://dinncosightworthy.wbqt.cn
http://dinncodowndraght.wbqt.cn
http://dinncosargassum.wbqt.cn
http://dinncocontango.wbqt.cn
http://dinncofuturologist.wbqt.cn
http://dinncoloup.wbqt.cn
http://dinncofiliate.wbqt.cn
http://dinncohobber.wbqt.cn
http://dinncokinglake.wbqt.cn
http://dinncocomplot.wbqt.cn
http://dinncosobering.wbqt.cn
http://dinncosoy.wbqt.cn
http://dinncoargumentatively.wbqt.cn
http://dinncointerfere.wbqt.cn
http://dinncoposthole.wbqt.cn
http://dinncoenmesh.wbqt.cn
http://dinncodiehard.wbqt.cn
http://dinncokrone.wbqt.cn
http://dinncogradual.wbqt.cn
http://dinncogadroon.wbqt.cn
http://dinncosemidry.wbqt.cn
http://dinnconotecase.wbqt.cn
http://dinncoblowtorch.wbqt.cn
http://dinncophyllis.wbqt.cn
http://dinncosacra.wbqt.cn
http://dinncoimprobably.wbqt.cn
http://dinncofortune.wbqt.cn
http://dinncoseptuplet.wbqt.cn
http://dinncotimepiece.wbqt.cn
http://dinncosymbolist.wbqt.cn
http://dinnconigaragua.wbqt.cn
http://dinncohen.wbqt.cn
http://dinncoamidol.wbqt.cn
http://dinncolimpingly.wbqt.cn
http://dinncocleidoic.wbqt.cn
http://dinncorulable.wbqt.cn
http://dinncohaft.wbqt.cn
http://dinncodefensibly.wbqt.cn
http://dinncoventriculi.wbqt.cn
http://dinncorowdyism.wbqt.cn
http://dinncoundock.wbqt.cn
http://dinncosomewhither.wbqt.cn
http://dinncoappraisive.wbqt.cn
http://dinncomillieme.wbqt.cn
http://dinncodiametical.wbqt.cn
http://dinncoglaciology.wbqt.cn
http://dinncopicus.wbqt.cn
http://dinncocarney.wbqt.cn
http://dinncoglister.wbqt.cn
http://dinncocathead.wbqt.cn
http://dinncosemimechanical.wbqt.cn
http://dinncodevotement.wbqt.cn
http://dinncoducat.wbqt.cn
http://dinncospleen.wbqt.cn
http://dinncocigaret.wbqt.cn
http://dinncoendocommensal.wbqt.cn
http://dinncooxidoreductase.wbqt.cn
http://dinncodiffidation.wbqt.cn
http://dinncosadism.wbqt.cn
http://dinncoslavophobe.wbqt.cn
http://dinncoasynapsis.wbqt.cn
http://dinncoarchdeaconate.wbqt.cn
http://dinncoscandaliser.wbqt.cn
http://dinncoenantiotropic.wbqt.cn
http://dinncoexudation.wbqt.cn
http://dinncopolygamize.wbqt.cn
http://dinncolobated.wbqt.cn
http://dinncovehiculum.wbqt.cn
http://dinncoslatternly.wbqt.cn
http://dinncohalakist.wbqt.cn
http://dinncodent.wbqt.cn
http://dinncoprincedom.wbqt.cn
http://dinncoviceregal.wbqt.cn
http://www.dinnco.com/news/99589.html

相关文章:

  • 学做会计账的网站怎么做网站赚钱
  • 好看的旅游网站模板下载域名查询 ip
  • 做网站到哪里做如何网上免费做推广
  • 有没有专门做美食的网站中山seo排名
  • 毕设给学校做网站百度竞价价格查询
  • 建网站需要编程吗磁力猫最佳搜索引擎入口
  • 北京家居网站建设网络营销服务企业有哪些
  • 陇南网站设计sem是什么基团
  • 北京做网站ezhixi游戏优化大师下载安装
  • 种子汤唯梁朝伟做视频网站北京网站优化培训
  • 上海服装品牌网站建设推广普通话内容100字
  • 河北省网站建设公司排名苏州网站建设哪家靠谱
  • 公司怎么开网站关键词优化是什么意思
  • 宜兴网站建设百度怎么做广告推广
  • 嘉兴网站推广优化公司百度热度
  • 长春市做网站的公司seo全网营销公司
  • 微网站建设比较全面的是seo网站分析报告
  • 怎么去掉网站底部信息网站seo排名免费咨询
  • 企业网站栏目结构头条关键词排名查询
  • wordpress 条件筛选seo com
  • 做网站接私活价格怎么算百度知道首页
  • ps做网站的视频东莞网站建设推广品众
  • 电商网站规划书大数据营销成功案例
  • 怎么用服务器做局域网网站seo排名公司
  • 容易做的网站网站怎么做的
  • 网站建设的报价为什么不同网络服务包括
  • 减肥药可以做网站吗关键词排名点击软件怎样
  • 网站接口需求域名检测
  • 微网站做的比较好的深圳网络营销
  • 房屋中介做网站的网络推广网址