欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2014年02月24日 09:58:59   作者:  
這篇文章主要介紹了使用curator實(shí)現(xiàn)zookeeper鎖服務(wù)的示例,需要的朋友可以參考下

復(fù)制代碼 代碼如下:

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();
   }
   }
 }
 }

相關(guān)文章

  • SpringBoot使用freemarker導(dǎo)出word文件方法詳解

    SpringBoot使用freemarker導(dǎo)出word文件方法詳解

    這篇文章主要介紹了SpringBoot使用freemarker導(dǎo)出word文件方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-11-11
  • java模擬微信搶紅包的實(shí)例代碼

    java模擬微信搶紅包的實(shí)例代碼

    現(xiàn)在搶紅包的功能很受歡迎,本篇文章主要介紹了java模擬微信搶紅包的實(shí)例代碼。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • Java導(dǎo)出excel時(shí)合并同一列中相同內(nèi)容的行思路詳解

    Java導(dǎo)出excel時(shí)合并同一列中相同內(nèi)容的行思路詳解

    這篇文章主要介紹了Java導(dǎo)出excel時(shí)合并同一列中相同內(nèi)容的行,需要的朋友可以參考下
    2018-06-06
  • Spring boot如何基于攔截器實(shí)現(xiàn)訪問權(quán)限限制

    Spring boot如何基于攔截器實(shí)現(xiàn)訪問權(quán)限限制

    這篇文章主要介紹了Spring boot如何基于攔截器實(shí)現(xiàn)訪問權(quán)限限制,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • spring security動(dòng)態(tài)配置url權(quán)限的2種實(shí)現(xiàn)方法

    spring security動(dòng)態(tài)配置url權(quán)限的2種實(shí)現(xiàn)方法

    對(duì)于使用spring security來說,存在一種需求,就是動(dòng)態(tài)去配置url的權(quán)限,即在運(yùn)行時(shí)去配置url對(duì)應(yīng)的訪問角色。下面這篇文章主要給大家介紹了關(guān)于spring security動(dòng)態(tài)配置url權(quán)限的2種實(shí)現(xiàn)方法,需要的朋友可以參考下
    2018-06-06
  • 深入了解Java核心類庫--BigDecimal和System類

    深入了解Java核心類庫--BigDecimal和System類

    這篇文章主要為大家詳細(xì)介紹了javaBigDecimal和System類定義與使用的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能給你帶來幫助
    2021-07-07
  • java與微信小程序?qū)崿F(xiàn)websocket長連接

    java與微信小程序?qū)崿F(xiàn)websocket長連接

    這篇文章主要為大家詳細(xì)介紹了java與微信小程序?qū)崿F(xiàn)websocket長連接,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Spring Security Remember me使用及原理詳解

    Spring Security Remember me使用及原理詳解

    這篇文章主要介紹了Spring Security Remember me使用及原理詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Default Methods實(shí)例解析

    Default Methods實(shí)例解析

    這篇文章主要介紹了Default Methods實(shí)例解析,介紹了默認(rèn)方法的相關(guān)問題,以及與普通方法的區(qū)別,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-09-09
  • Java堆空間爆滿導(dǎo)致宕機(jī)的問題分析及解決

    Java堆空間爆滿導(dǎo)致宕機(jī)的問題分析及解決

    團(tuán)隊(duì)有一個(gè)服務(wù),一直運(yùn)行的好好的,突然訪問異常了,先是請求超時(shí),然后直接無法訪問,本文將給大家介紹Java堆空間爆滿導(dǎo)致宕機(jī)的問題分析及解決,需要的朋友可以參考下
    2024-02-02

最新評(píng)論