一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - 使用curator實(shí)現(xiàn)zookeeper鎖服務(wù)的示例分享

使用curator實(shí)現(xiàn)zookeeper鎖服務(wù)的示例分享

2019-11-08 14:31java教程網(wǎng) JAVA教程

這篇文章主要介紹了使用curator實(shí)現(xiàn)zookeeper鎖服務(wù)的示例,需要的朋友可以參考下

代碼如下:


import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

 

import com.netflix.curator.RetryPolicy;
import com.netflix.curator.framework.CuratorFramework;
import com.netflix.curator.framework.CuratorFrameworkFactory;
import com.netflix.curator.framework.recipes.locks.InterProcessMutex;
import com.netflix.curator.retry.ExponentialBackoffRetry;

public class TestCuratorLock {

 /**
  * @param args
  * @throws InterruptedException
  */
 public static void main(String[] args) throws InterruptedException {
  // TODO Auto-generated method stub

  CountDownLatch latch = new CountDownLatch(5);

  String zookeeperConnectionString = "localhost:2181,localhost:2182,localhost:2183";
  RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
  CuratorFramework client = CuratorFrameworkFactory.newClient(
    zookeeperConnectionString, retryPolicy);
  client.start();
  System.out.println("客戶端啟動(dòng)。。。。");
  ExecutorService exec = Executors.newCachedThreadPool();

  for (int i = 0; i < 5; i++) {
   exec.submit(new MyLock("client" + i, client, latch));
  }

  exec.shutdown();
  latch.await();
  System.out.println("所有任務(wù)執(zhí)行完畢");

  client.close();

  System.out.println("客戶端關(guān)閉。。。。");

 }

 static class MyLock implements Runnable {

  private String name;

  private CuratorFramework client;

  private CountDownLatch latch;

  public MyLock(String name, CuratorFramework client, CountDownLatch latch) {
   this.name = name;
   this.client = client;
   this.latch = latch;
  }

  public String getName() {
   return name;
  }

  public void setName(String name) {
   this.name = name;
  }

  @Override
  public void run() {
   // TODO Auto-generated method stub
   InterProcessMutex lock = new InterProcessMutex(client,
     "/test_group");
   try {
    if (lock.acquire(120, TimeUnit.SECONDS)) {
     try {
      // do some work inside of the critical section here
      System.out.println("----------" + this.name
        + "獲得資源----------");
      System.out.println("----------" + this.name
        + "正在處理資源----------");
      Thread.sleep(10 * 1000);
      System.out.println("----------" + this.name
        + "資源使用完畢----------");
      latch.countDown();
     } finally {
      lock.release();
      System.out.println("----------" + this.name
        + "釋放----------");
     }
    }
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   }
 }
 }

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 草莓秋葵菠萝蜜绿巨人污 | 亚洲国产成人久久精品hezyo | 欧美二区视频 | 出水小说 | 人与蛇boxxⅹ | 果冻传媒 天美 麻豆 | 久久久久嫩草影院精品 | 日韩免费高清专区 | 国产精品久久久久一区二区三区 | 69一级毛片| 亚洲精品国产综合久久一线 | 欧美一二区 | 亚洲+国产+图片 | 海角社区在线登录 | 白丝vk丨tk失禁 | bedfriend泰剧全集免费观看 | 婷婷日韩 | 小sao货水好多真紧h的视频 | 日韩毛片免费 | 精品国产自在现线拍国语 | 精品四虎国产在免费观看 | 国产自在线观看 | 日本邪恶动态 | 深夜日韩| 强行扒开美女大腿挺进 | 亚欧视频在线观看 | 男同gay作爰视频网站 | 精品伊人 | 7777奇米| 91视频破解版 | 视频一本大道香蕉久在线播放 | 五月天色小说 | 99久久爱热6在线播放 | 国产一卡二卡3卡4卡四卡在线 | 日本伊人色 | 日韩毛片基地一区二区三区 | 青青青青青国产免费手机看视频 | jazz中国在线视频 | 外国a级片 | 日本一区二区视频在线观看 | 国产综合色在线视频区色吧图片 |