zk-rw-lock/src/main/java/io/github/ehlxr/zkrwlock/InterProcessReadWriteLockTe...

193 lines
6.8 KiB
Java
Raw Normal View History

2021-02-03 15:38:52 +00:00
/*
* The MIT License (MIT)
*
* Copyright © 2020 xrv <xrg@live.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.ehlxr.zkrwlock;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.imps.CuratorFrameworkState;
import org.apache.curator.framework.recipes.locks.InterProcessLock;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock;
import org.apache.curator.retry.RetryOneTime;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;
/**
* @author ehlxr
* @since 2021-02-03 22:42.
*/
public class InterProcessReadWriteLockTest {
2021-02-04 02:56:12 +00:00
private static final CuratorFramework ZK_CLIENT;
2021-02-03 15:38:52 +00:00
2021-02-04 02:56:12 +00:00
static {
2021-02-03 15:38:52 +00:00
ZK_CLIENT = CuratorFrameworkFactory.builder()
.connectString("localhost:2181")
.sessionTimeoutMs(2000000)
.retryPolicy(new RetryOneTime(10000))
.namespace("lock")
.build();
ZK_CLIENT.start();
if (ZK_CLIENT.getState() == CuratorFrameworkState.STARTED) {
System.out.println("启动成功");
}
}
2021-02-04 02:56:12 +00:00
public static void main(String[] args) throws Exception {
InterProcessReadWriteLockTest interProcessReadWriteLockTest = new InterProcessReadWriteLockTest();
interProcessReadWriteLockTest.reentrantReadLockTest();
interProcessReadWriteLockTest.wwTest();
interProcessReadWriteLockTest.rwLockTest();
}
2021-02-04 02:49:08 +00:00
public void reentrantReadLockTest() {
2021-02-03 15:38:52 +00:00
int num = 2;
2021-02-04 02:49:08 +00:00
// CountDownLatch latch = new CountDownLatch(num);
2021-02-03 15:38:52 +00:00
IntStream.range(0, num).forEach(i -> {
// 创建共享可重入读锁
InterProcessLock readLock = new InterProcessReadWriteLock(ZK_CLIENT, "/test").readLock();
try {
2021-02-04 02:49:08 +00:00
// 获取锁对象
2021-02-03 15:38:52 +00:00
readLock.acquire();
System.out.println(i + "获取读锁===============");
// 测试锁重入
readLock.acquire();
System.out.println(i + "再次获取读锁===============");
Thread.sleep(2000);
readLock.release();
System.out.println(i + "释放读锁===============");
readLock.release();
System.out.println(i + "再次释放读锁===============");
2021-02-04 02:49:08 +00:00
// latch.countDown();
2021-02-03 15:38:52 +00:00
} catch (Exception e) {
e.printStackTrace();
}
});
2021-02-04 02:49:08 +00:00
// latch.await();
2021-02-03 15:38:52 +00:00
}
public void rwLockTest() throws Exception {
/*
* 线
*/
int num = 50;
CyclicBarrier barrier = new CyclicBarrier(num);
2021-02-04 02:49:08 +00:00
ExecutorService pool = Executors.newFixedThreadPool(num);
CountDownLatch latch = new CountDownLatch(num);
2021-02-03 15:38:52 +00:00
/*
* "+ 开始读请求。。。。" "= 读请求结束。。。。"
*/
2021-02-04 02:49:08 +00:00
IntStream.range(0, num).forEach(i -> pool.execute(() -> {
2021-02-03 15:38:52 +00:00
InterProcessMutex lock = new InterProcessReadWriteLock(ZK_CLIENT, "/test").readLock();
try {
System.out.println("> 读请求就绪。。。。" + i + " " + Thread.currentThread().getName());
barrier.await();
lock.acquire();
System.out.println("+ 开始读请求。。。。" + i + " " + Thread.currentThread().getName());
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("= 读请求结束。。。。" + i + " " + Thread.currentThread().getName());
try {
lock.release();
} catch (Exception e) {
e.printStackTrace();
}
latch.countDown();
}
}));
2021-02-04 02:49:08 +00:00
Thread.sleep(10);
2021-02-03 15:38:52 +00:00
InterProcessMutex lock = new InterProcessReadWriteLock(ZK_CLIENT, "/test").writeLock();
try {
lock.acquire();
System.out.println("\n开始写请求。。。。");
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("写请求结束。。。。\n");
lock.release();
}
latch.await();
pool.shutdown();
}
2021-02-04 02:49:08 +00:00
public void wwTest() {
2021-02-03 15:38:52 +00:00
int num = 5;
2021-02-04 02:49:08 +00:00
// CountDownLatch latch = new CountDownLatch(num);
2021-02-03 15:38:52 +00:00
CyclicBarrier barrier = new CyclicBarrier(num);
/*
* "+ 开始写请求。。。。" "= 写请求结束。。。。"
*/
IntStream.range(0, num).parallel().forEach(i -> {
InterProcessMutex lock = new InterProcessReadWriteLock(ZK_CLIENT, "/test").writeLock();
try {
System.out.println("> 写请求就绪。。。。" + i + " " + Thread.currentThread().getName());
barrier.await();
lock.acquire();
System.out.println("\n+ 开始写请求。。。。" + i + " " + Thread.currentThread().getName());
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("= 写请求结束。。。。" + i + " " + Thread.currentThread().getName());
try {
lock.release();
} catch (Exception e) {
e.printStackTrace();
}
2021-02-04 02:49:08 +00:00
// latch.countDown();
2021-02-03 15:38:52 +00:00
}
});
2021-02-04 02:49:08 +00:00
// latch.await();
2021-02-03 15:38:52 +00:00
}
}