使用Zookeeper实现分布式共享锁(3)

下面使用这个工具来测试一下我们的分布式共享锁

package com.concurrent;

import com.concurrent.ConcurrentTest.ConcurrentTask;

public class ZkTest {
 public static void main(String[] args) {
  Runnable task1 = new Runnable(){
   public void run() {
    DistributedLock lock = null;
    try {
     lock = new DistributedLock("127.0.0.1:2182","test1");
     //lock = new DistributedLock("127.0.0.1:2182","test2");
     lock.lock();
     Thread.sleep(3000);
     System.out.println("===Thread " + Thread.currentThread().getId() + " running");
    } catch (Exception e) {
     e.printStackTrace();
    }
    finally {
     if(lock != null)
      lock.unlock();
    }
   
   }
   
  };
  new Thread(task1).start();
  try {
   Thread.sleep(1000);
  } catch (InterruptedException e1) {
   e1.printStackTrace();
  }
  ConcurrentTask[] tasks = new ConcurrentTask[10];
  for(int i=0;i<tasks.length;i++){
   ConcurrentTask task3 = new ConcurrentTask(){
    public void run() {
     DistributedLock lock = null;
     try {
      lock = new DistributedLock("127.0.0.1:2183","test2");
      lock.lock();
      System.out.println("Thread " + Thread.currentThread().getId() + " running");
     } catch (Exception e) {
      e.printStackTrace();
     }
     finally {
      lock.unlock();
     }
     
    }
   };
   tasks[i] = task3;
  }
  new ConcurrentTest(tasks);
 }
}

测试结果:

/locks/test1_lock_0000004356 is created
/locks/test1_lock_0000004356==test1_lock_0000004356
Thread 8 /locks/test1_lock_0000004356 get lock true
/locks/test2_lock_0000004357 is created
/locks/test2_lock_0000004359 is created
/locks/test2_lock_0000004358 is created
/locks/test2_lock_0000004363 is created
/locks/test2_lock_0000004361 is created
/locks/test2_lock_0000004360 is created
/locks/test2_lock_0000004362 is created
/locks/test2_lock_0000004366 is created
/locks/test2_lock_0000004365 is created
/locks/test2_lock_0000004364 is created
/locks/test2_lock_0000004357==test2_lock_0000004357
Thread 14 /locks/test2_lock_0000004357 get lock true
Thread 14 running
unlock /locks/test2_lock_0000004357
/locks/test2_lock_0000004358==test2_lock_0000004357
/locks/test2_lock_0000004361==test2_lock_0000004357
/locks/test2_lock_0000004359==test2_lock_0000004357
/locks/test2_lock_0000004362==test2_lock_0000004357
Thread 12 waiting for /locks/test2_lock_0000004360
/locks/test2_lock_0000004366==test2_lock_0000004357
Thread 18 waiting for /locks/test2_lock_0000004357
/locks/test2_lock_0000004363==test2_lock_0000004357
Thread 18 running
unlock /locks/test2_lock_0000004358
Thread 13 waiting for /locks/test2_lock_0000004362
/locks/test2_lock_0000004365==test2_lock_0000004358
Thread 16 waiting for /locks/test2_lock_0000004361
Thread 19 waiting for /locks/test2_lock_0000004358
/locks/test2_lock_0000004360==test2_lock_0000004358
Thread 15 waiting for /locks/test2_lock_0000004365
/locks/test2_lock_0000004364==test2_lock_0000004358
Thread 11 waiting for /locks/test2_lock_0000004364
Thread 20 waiting for /locks/test2_lock_0000004359
Thread 19 running
unlock /locks/test2_lock_0000004359
Thread 17 waiting for /locks/test2_lock_0000004363
Thread 20 running
unlock /locks/test2_lock_0000004360
Thread 12 running
unlock /locks/test2_lock_0000004361
Thread 16 running
unlock /locks/test2_lock_0000004362
Thread 13 running
unlock /locks/test2_lock_0000004363
Thread 17 running
unlock /locks/test2_lock_0000004364
Thread 11 running
unlock /locks/test2_lock_0000004365
Thread 15 running
unlock /locks/test2_lock_0000004366
min: 506
max: 1481
avg: 968
err: 0
===Thread 8 running
unlock /locks/test1_lock_0000004356

关于zookeeper的很好的文章:

这个分布式共享锁就是参考这篇文章实现的。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/d68ce6970ca4698852f57d80b2d23629.html