update at 2021-02-04 10:49:08 by ehlxr
parent
a329e2dea3
commit
194ff772d6
|
@ -65,16 +65,16 @@ public class InterProcessReadWriteLockTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void reentrantReadLockTest() throws Exception {
|
public void reentrantReadLockTest() {
|
||||||
int num = 2;
|
int num = 2;
|
||||||
CountDownLatch latch = new CountDownLatch(num);
|
// CountDownLatch latch = new CountDownLatch(num);
|
||||||
|
|
||||||
IntStream.range(0, num).forEach(i -> {
|
IntStream.range(0, num).forEach(i -> {
|
||||||
// 创建共享可重入读锁
|
// 创建共享可重入读锁
|
||||||
InterProcessLock readLock = new InterProcessReadWriteLock(ZK_CLIENT, "/test").readLock();
|
InterProcessLock readLock = new InterProcessReadWriteLock(ZK_CLIENT, "/test").readLock();
|
||||||
|
|
||||||
// 获取锁对象
|
|
||||||
try {
|
try {
|
||||||
|
// 获取锁对象
|
||||||
readLock.acquire();
|
readLock.acquire();
|
||||||
System.out.println(i + "获取读锁===============");
|
System.out.println(i + "获取读锁===============");
|
||||||
// 测试锁重入
|
// 测试锁重入
|
||||||
|
@ -86,13 +86,13 @@ public class InterProcessReadWriteLockTest {
|
||||||
readLock.release();
|
readLock.release();
|
||||||
System.out.println(i + "再次释放读锁===============");
|
System.out.println(i + "再次释放读锁===============");
|
||||||
|
|
||||||
latch.countDown();
|
// latch.countDown();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
latch.await();
|
// latch.await();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -101,15 +101,15 @@ public class InterProcessReadWriteLockTest {
|
||||||
* 读线程不互斥
|
* 读线程不互斥
|
||||||
*/
|
*/
|
||||||
int num = 50;
|
int num = 50;
|
||||||
CountDownLatch latch = new CountDownLatch(num);
|
|
||||||
CyclicBarrier barrier = new CyclicBarrier(num);
|
CyclicBarrier barrier = new CyclicBarrier(num);
|
||||||
|
|
||||||
ExecutorService pool = Executors.newFixedThreadPool(1);
|
ExecutorService pool = Executors.newFixedThreadPool(num);
|
||||||
|
CountDownLatch latch = new CountDownLatch(num);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* "+ 开始读请求。。。。" 与 "= 读请求结束。。。。" 交叉出现
|
* "+ 开始读请求。。。。" 与 "= 读请求结束。。。。" 交叉出现
|
||||||
*/
|
*/
|
||||||
pool.execute(() -> IntStream.range(0, num).parallel().forEach(i -> {
|
IntStream.range(0, num).forEach(i -> pool.execute(() -> {
|
||||||
InterProcessMutex lock = new InterProcessReadWriteLock(ZK_CLIENT, "/test").readLock();
|
InterProcessMutex lock = new InterProcessReadWriteLock(ZK_CLIENT, "/test").readLock();
|
||||||
try {
|
try {
|
||||||
System.out.println("> 读请求就绪。。。。" + i + " " + Thread.currentThread().getName());
|
System.out.println("> 读请求就绪。。。。" + i + " " + Thread.currentThread().getName());
|
||||||
|
@ -134,6 +134,7 @@ public class InterProcessReadWriteLockTest {
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
Thread.sleep(10);
|
||||||
InterProcessMutex lock = new InterProcessReadWriteLock(ZK_CLIENT, "/test").writeLock();
|
InterProcessMutex lock = new InterProcessReadWriteLock(ZK_CLIENT, "/test").writeLock();
|
||||||
try {
|
try {
|
||||||
lock.acquire();
|
lock.acquire();
|
||||||
|
@ -152,9 +153,9 @@ public class InterProcessReadWriteLockTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void wwTest() throws InterruptedException {
|
public void wwTest() {
|
||||||
int num = 5;
|
int num = 5;
|
||||||
CountDownLatch latch = new CountDownLatch(num);
|
// CountDownLatch latch = new CountDownLatch(num);
|
||||||
CyclicBarrier barrier = new CyclicBarrier(num);
|
CyclicBarrier barrier = new CyclicBarrier(num);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -181,10 +182,10 @@ public class InterProcessReadWriteLockTest {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
latch.countDown();
|
// latch.countDown();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
latch.await();
|
// latch.await();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue