zookeeper锁测试
pom引入相应的jar包
<dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <version>5.1.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.13</version> </dependency>
java测试类
package com.yydone.demo; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.recipes.locks.InterProcessMutex; import org.apache.curator.retry.RetryOneTime; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ZkLockTest { private static ExecutorService service; static final CuratorFramework curator; static final InterProcessMutex zkMutex; static { curator = CuratorFrameworkFactory.newClient("10.6.124.17:2181,10.6.124.18:2181,10.6.124.19:2181", new RetryOneTime(2000)); curator.start(); zkMutex = new InterProcessMutex(curator, "/mutex"); } public static void count(int threadNum, final int workers) throws InterruptedException { final CountDownLatch latch = new CountDownLatch(threadNum); service = Executors.newFixedThreadPool(threadNum); long start = System.currentTimeMillis(); for(int i=0; i< threadNum; ++i) { service.execute(new Runnable() { public void run() { for(int i=0; i< workers; ++i) { try { zkMutex.acquire(); } catch (Exception e) { e.printStackTrace(); } finally { try { zkMutex.release(); } catch (Exception e) { e.printStackTrace(); } } } latch.countDown(); } }); } service.shutdown(); latch.await(); long end = System.currentTimeMillis(); System.out.println("Thread Num:" + threadNum + "workers per Thread:" + workers + " cost time:" + (end - start) + " avg" + (threadNum * workers)*1000/(end-start)); } public static void main(String []args) throws Exception { ZkLockTest.count(1, 500); ZkLockTest.count(10, 500); } }
运行结果
Thread Num:1workers per Thread:500 cost time:47199 avg10
Thread Num:10workers per Thread:500 cost time:429963 avg11