public static void main(String[] args) {
CountDownLatch countDownLatch = new CountDownLatch(3);
for (int i = 0 ; i < 3 ; i++) {
int count = i;
new Thread(() -> {
System.out.println("Count down thread id : " + count);
countDownLatch.countDown();
}).start();
}
try {
System.out.println("Main thread await!");
countDownLatch.await();
System.out.println("Main thread finish!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
从日志输出可以看到,主线程进入等待,当所有子线程均调用了countDown后,主线程继续执行:
Main thread await! Count down thread id : 1 Count down thread id : 0 Count down thread id : 2 Main thread finish!
public static void main(String[] args) {
CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
for (int i = 0; i < 3; i++) {
int count = i;
new Thread(() -> {
try {
Thread.sleep(3L);
System.out.println("Thread id waiting : " + count);
cyclicBarrier.await();
System.out.println("Thread id finish : " + count);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}).start();
}
}
从日志可以看到,所有线程首先输出waiting,然后再输出finish:
Thread id waiting : 2 Thread id waiting : 0 Thread id waiting : 1 Thread id finish : 1 Thread id finish : 2 Thread id finish : 0
public static void main(String[] args) {
Semaphore semaphore = new Semaphore(3);
for (int i = 0 ; i < 5 ; i++) {
int count = i;
new Thread(() -> {
try {
semaphore.acquire();
System.out.println("Thread got semaphore : " + count);
Thread.sleep(2L);
semaphore.release();
System.out.println("Thread release semaphore : " + count);
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
从日志中可以看出,0、1、2共3个线程首先获取了锁,随后1首先释放锁,释放后3获取了锁,获取锁的总线程仍为3。随后0释放了锁,此时4就可以获取锁,执行的总线程仍为3。
Thread got semaphore : 0 Thread got semaphore : 1 Thread got semaphore : 2 Thread release semaphore : 1 Thread got semaphore : 3 Thread release semaphore : 0 Thread got semaphore : 4 Thread release semaphore : 2 Thread release semaphore : 4 Thread release semaphore : 3