CyclicBarrier

 

 

public class CyclicBarrierTest {

	 
	//After the first 4 threads cross the barrier state, they can be used for a new round of use. And CountDownLatch cannot be reused.
	public static void main(String[] args) throws InterruptedException {
		 ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(10);
		int num = 5 ;
		CyclicBarrier cyclicBarrier = new CyclicBarrier(num);
		for (int i = 0; i < 5; i++) {
			newFixedThreadPool.submit(new CyclicBarrierRunnable(cyclicBarrier));
		}
		System.out.println("==========end===1========"+System.currentTimeMillis());
        
		Thread.sleep(5000);
		
		for (int i = 0; i < 5; i++) {
			newFixedThreadPool.submit(new CyclicBarrierRunnable(cyclicBarrier));
		}
		System.out.println("==========end===2========"+System.currentTimeMillis());
		
		newFixedThreadPool.shutdown();
	}
	
	public static class CyclicBarrierRunnable implements Runnable{

		private CyclicBarrier cyclicBarrier;
		
		public CyclicBarrierRunnable(CyclicBarrier cyclicBarrier) {
			this.cyclicBarrier = cyclicBarrier;
		}

		@Override
		public void run() {
			try {
				cyclicBarrier.await();
				Thread.sleep(1000);
				System.out.println("====================="+System.currentTimeMillis());
                 
                 
			} catch (InterruptedException e) {
				e.printStackTrace ();
			} catch (BrokenBarrierException e) {
				e.printStackTrace ();
			}
		}
		
	}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Donate to developers 

Driven by interest, I write 免费something with joy and sweat. I hope you like my work and can support it at the same time. Of course, if you have money to support a money field (support Alipay, WeChat, and the buckle group), if you have no money to support a personal field, thank you.

 

Personal homepage : http://knight-black-bob.iteye.com/



 
 
 Thank you for your sponsorship, I will do better!

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326314596&siteId=291194637