//输出结果 Ready to start Zk Waiting zks status ZKs are working Trying to start kafka
CyclicBarrier
A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. CyclicBarriers are useful in programs involving a fixed sized party of threads that must occasionally wait for each other. The barrier is called cyclic because it can be re-used after the waiting threads are released.
for (int i=0; i<3 ;i++){ newThread(newRunnable() { @Override publicvoidrun() { try { System.out.println("A new colleague come in"); // 等待所有人到齐,开始会议 cyclicBarrier.await(); System.out.println("All teams members are ready. Start the meeting");
//Output A newcolleague come in A newcolleague come in A newcolleague come in All teams members are ready. Start the meeting All teams members are ready. Start the meeting All teams members are ready. Start the meeting I am tired I am tired I am tired Have a rest Have a rest Have a rest
Semaphore
Semaphores are often used to restrict the number of threads than can access some (physical or logical) resource.