pom introduces the corresponding jar package
<dependency>
<groupId>org.junit.vintage< span style="color: #800000">groupId>
<artifactId>junit-vintage-engine< /artifactId>
<version>5.1.1version>
<< /span>scope>test< /span>scope>
dependency>
<dependency>
<groupId>org.apache. curatorgroupId>
<artifactId> span>curator-frameworkartifactId> < /span>
<version >4.0.0version>
dependency>
<dependency >
<groupId span>>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>4.0.0version>
< span style="color: #800000">dependency>
<dependency>
<< /span>groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.13version>
< span style="color: #0000ff">dependency> pre>
java test class
package com.yydone.demo;
import org .apache.curator.framework.CuratorFramework;
import org.apache.curator. framework.CuratorFrameworkFactory;
import org.apache.cu rator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator .retry.RetryOneTime;
import java.util.concurrent. CountDownLatch;
import java.util.concurrent.ExecutorService;
< /span>import java.util.concurrent.Executors;
public class ZkLockTest {
< /span>private static ExecutorService service;< br /> 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, < span style="color: #0000ff">final int workers) throws< span style="color: #000000 "> InterruptedException {
final CountDownLatch latch = new CountDownLatch(threadNum);
service = Executors.newFixedThreadPool(threadNum);
< span style="color: #0000ff">long start = System.currentTimeMillis();
for(int i=0; i< threadNum; ++i) {< br /> service.execute(new Runnable() {
public void run() {
for(int i=0; i< workers; ++i) {
try {
zkMutex.acquire();
} catch (Exception e) {< br /> e.printStackTrace();
} finally {
try {
zkMutex.release();
} catch (Exception e) {
e.printStackTrace();
}
}
}
latch.countDown();
}
});
}
service.shutdown();
latch.await();
long span> 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);
}
}< /pre>
Run results
Thread Num:1worke rs per Thread:500 cost time:47199 avg10
Thread Num:10workers per Thread:500 cost time:429963 avg11
<dependency>
<groupId>org.junit.vintage< span style="color: #0000ff">groupId>
<artifactId> junit-vintage-engineartifactId>
<version>5.1.1version>
<scope >testscope >
dependency< /span>>
<dependency>
<groupId>org.apache.curator< span style="color: #800000">groupId>
<artifactId>curator-framework< /span>artifactId >
<version>4.0.0< /span>version>
dependency>
<dependency>
<groupId>org.apache. curatorgroupId>
<artifactId> span>curat or-recipesartifactId> span>
<version> 4.0.0version>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId< /span>>zookeeperartifactId< /span>>
< version>3.4.13version>
dependency>
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< /span> InterProcessMutex(curator, "/mutex");
}
public static void count(int threadNum, final int workers) thr ows InterruptedException {
final CountDownLatch latch = new CountDownLatch(threadNum);
service = Executors. newFixedThreadPool(threadNum);
long start = System.currentTimeMillis();
for(int i=0; i< threadNum; ++< span style="color: #000000">i) {
service.execute(new Runnable() {
public void run() {
for(int i=0; i< workers; ++i) {
try span> {
zkMutex.acquire();
} catch< span style="color: #000000"> (Exception e) {
e.printStackTrace();
} finally {
try {
zkMutex.release();
} catch (Exception e) {< br /> 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);
}
}