1、ReentrantLock
public class LockTest {
static Map<String, Integer> map = new ConcurrentHashMap<String, Integer>();
Lock lock = new ReentrantLock();
private void testReentrantLock() {
try {
lock.lock();
} catch (Throwable e) {
} finally {
lock.unlock();
}
}
2、CountDownLatch
private static void testCountDownLatch() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(2);
Thread thread = new Thread(() -> {
System.out.println("1111");
countDownLatch.countDown();
}, "12345678");
Thread thread2 = new Thread(() -> {
System.out.println("22222");
countDownLatch.countDown();
}, "2345678");
thread2.start();
thread.start();
countDownLatch.await();
System.out.println("123456789");
}
3、CyclicBarrier
private static void testCyclicBarrier() {
CyclicBarrier cyclicBarrier = new CyclicBarrier(5, () -> {
Set<String> set = map.keySet();
System.out.println(set.size());
//Set<Map.Entry<String,Integer>> entrySet=map.entrySet();
// for(Map.Entry<String,Integer> a:entrySet){
// System.out.println(a.getKey());
// System.out.println(a.getValue());
// }
});
for (int i = 0; i < 100; i++) {
new Thread(() -> {
try {
map.put(Thread.currentThread().getName(), 12345);
cyclicBarrier.await();
map.put(Thread.currentThread().getName(), 12345);
//barrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}).start();
}
// System.out.println("123456789");
}
4、Phaser
private static void testPhaser() {
Phaser phaser = new Phaser(2);
Thread thread = new Thread(() -> {
System.out.println("1111");
phaser.arrive();
}, "12345678");
Thread thread2 = new Thread(() -> {
System.out.println("22222");
phaser.arrive();
}, "2345678");
thread2.start();
thread.start();
phaser.awaitAdvance(phaser.getPhase());
System.out.println("123456789");
}
private static void testPhaser2() {
CountDownLatch countDownLatch = new CountDownLatch(1);
Phaser phaser = new Phaser(5);
for (int i = 0; i < 100; i++) {
new Thread(() -> {
if (map.size() == 5) {
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
map.put(Thread.currentThread().getName(), 1234);
System.out.println("111111111");
phaser.arriveAndAwaitAdvance();
System.out.println("=========");
if (map.size() == 5) {
countDownLatch.countDown();
}
}).start();
}
}
5、ReadWriteLock
private static void testReadWriteLock() {
ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
Lock readLock = readWriteLock.readLock();
Lock writeLock = readWriteLock.writeLock();
for (int i = 0; i < 10; i++) {
new Thread(() -> {
readLock.lock();
System.out.println("read start");
System.out.println("read end");
readLock.unlock();
}, "read").start();
}
for (int i = 0; i < 5; i++) {
new Thread(() -> {
writeLock.lock();
System.out.println("write start");
System.out.println("write end");
writeLock.unlock();
}, "write").start();
}
}
6、Semaphore
private static void testSemaphore() {
Semaphore semaphore = new Semaphore(2, true);
for (int i = 0; i < 5; i++) {
new Thread(() -> {
try {
semaphore.acquire();
System.out.println("1111111111");
TimeUnit.SECONDS.sleep(1);
System.out.println("==========");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
semaphore.release();
}
}, "write").start();
}
}
7、Exchanger
private static void testExchanger() {
Exchanger<String> exchanger = new Exchanger<>();
new Thread(() -> {
String a = "a";
try {
System.out.println(Thread.currentThread().getName() + " " + a);
a = exchanger.exchange(a);
System.out.println(Thread.currentThread().getName() + " " + a);
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "read").start();
new Thread(() -> {
String a = "b";
try {
System.out.println(Thread.currentThread().getName() + " " + a);
a = exchanger.exchange(a);
System.out.println(Thread.currentThread().getName() + " " + a);
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "write").start();
}
8、LockSupport
private static void testLockSupport() {
Thread a =new Thread(() -> {
System.out.println("1111");
LockSupport.park();
System.out.println("2222");
}, "123");
a.start();
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread b =new Thread(() -> {
System.out.println("3333");
LockSupport.unpark(a);
System.out.println("4444");
}, "1234");
b.start();
}
测试方法
public static void main(String[] args) throws InterruptedException {
//testCountDownLatch();
//testCyclicBarrier();
//testPhaser();
//testPhaser2();
//testReadWriteLock();
//testSemaphore();
//testExchanger();
//testLockSupport();
testLockSupport();
}
}