CyclicBarrier和CountDownLatch都用多个线程之间的同步,
共同点:
同时有N个线程在 CyclicBarrier(CountDownLatch) 等待上等待时,CyclicBarrier(CountDownLatch) 的wait 函数满足条件,会唤醒等待线程,从 等待线程的wait()出返回。
不同点:
CountDownLatch latch=new CountDownLatch(2);
thread1:latch.wait() .不满足 ,线程切换到等待状态
thread2:latch.wait() 满足, 此时 thread2从wait出返回,并且唤醒 thread1线程,
thread3:latch.wait() 此时,无条件满足,直接返回,线程不会切换出执行状态
CyclicBarrier latch=new CyclicBarrier (2);
thread1:cyclicBarrier.wait() .不满足 ,线程切换到等待状态
thread2:cyclicBarrier.wait() 满足, 此时 thread2从wait出返回,并且唤醒 thread1线程,
thread3:cyclicBarrier.wait() 依旧不满足,thread3 切换到等待状态。
thread4:cyclicBarrier.wait() 满足, 此时 thread4从wait出返回,并且唤醒 thread3线。