乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      使用Zookeeper實(shí)現(xiàn)分布式鎖

       印度阿三17 2019-07-19

      利用Zookeeper臨時(shí)節(jié)點(diǎn)(客戶端異常斷開連接后臨時(shí)節(jié)點(diǎn)自動(dòng)移除)或者Redis SETNX(set if not exists)(設(shè)置ttl)可以實(shí)現(xiàn)分布式鎖,這里先利用zk實(shí)現(xiàn)一個(gè)

      1.啟動(dòng)zk

      ?

      2.代碼中watch節(jié)點(diǎn)

      ?  2.1 Maven引入zk & zk client

        

        2.2 代碼和注釋

      import org.I0Itec.zkclient.ZkClient;
      
      import java.util.concurrent.CountDownLatch;
      
      public class ZKDistributeLockTest {
      
          public static void main(String[] args) {
              // 使用CountDownLunch控制線程同時(shí)執(zhí)行
              CountDownLatch countDownLatch = new CountDownLatch(1);
              // 開啟3個(gè)線程模擬分布式環(huán)境,分布式環(huán)境下每個(gè)進(jìn)程都是一個(gè)單獨(dú)的zkClient
              Thread t1 = new Thread(new TestThread(countDownLatch));
              Thread t2 = new Thread(new TestThread(countDownLatch));
              Thread t3 = new Thread(new TestThread(countDownLatch));
              t1.start();
              t2.start();
              t3.start();
      
              System.out.println("休眠1秒后執(zhí)行..."   System.currentTimeMillis());
              try {
                  Thread.sleep(1000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              // 倒計(jì)時(shí)結(jié)束
              countDownLatch.countDown();
          }
      
      
      }
      
      // 線程,嘗試在zk上創(chuàng)建臨時(shí)節(jié)點(diǎn),創(chuàng)建成功則獲得鎖(執(zhí)行權(quán))
      class TestThread implements Runnable {
          // 共享變量
          private static Integer CNT = 0;
          private ZkClient zkClient;
          private CountDownLatch countDownLatch;
          public TestThread(CountDownLatch countDownLatch) {
              this.countDownLatch = countDownLatch;
          }
      
          // 連接zk
          private void connect() {
              String threadName = Thread.currentThread().getName();
              try {
                  System.out.println(threadName   " 等待執(zhí)行...");
                  // 等待倒計(jì)時(shí)結(jié)束
                  countDownLatch.await();
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              System.out.println(threadName   " 請(qǐng)求連接zk..."   System.currentTimeMillis());
              zkClient = new ZkClient("192.168.1.217:2181", 20000);
              System.out.println(threadName   " 連接成功...");
              // 輸出目錄信息測(cè)試
      //        List<String> children = zkClient.getChildren("/");
      //        children.forEach(System.out::println);
          }
      
          @Override
          public void run() {
              // 初始化連接(在各個(gè)線程里開啟連接,模擬分布式環(huán)境)
              connect();
              String threadName = Thread.currentThread().getName();
      
              // 競(jìng)爭(zhēng)鎖
              while (true) {
                  try {
                      System.out.println(threadName   " 開始競(jìng)爭(zhēng)鎖...");
                      // 創(chuàng)建zk臨時(shí)節(jié)點(diǎn)
                      zkClient.createEphemeral("/dl", "test");
                      System.out.println(threadName   " 獲得鎖?。?!");
                      // 獲得鎖后修改共享變量
                      CNT   ;
                      System.out.println(threadName   " 釋放了鎖..."   CNT);
                      zkClient.delete("/dl");
                      Thread.sleep(2000);
                  } catch (Exception e) {
                      // 創(chuàng)建臨時(shí)節(jié)點(diǎn)失敗,表示未獲得鎖
                      System.out.println(threadName   " 未獲得鎖,將重試?。。?);
      //                System.out.println(e.getMessage());
                      try {
                          Thread.sleep(1500);
                      } catch (InterruptedException e1) {
                          e1.printStackTrace();
                      }
                  }
              }
          }
      }
      

        

      3.測(cè)試結(jié)果

      ?

      來源:https://www./content-4-339151.html

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多