CyclicBarrier一个简单Demo

package thread;

import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierDemo {
	public static void main(String[] args) {
		CyclicBarrier barrier = new CyclicBarrier(10,new Runnable(){
			public void run(){
				System.out.println("finished...");
			}

		});
		for(int i=0;i<10;i++){
			new Thread(new Work(barrier,i)).start();
		}
		
	}
}
class Work implements Runnable{
	private CyclicBarrier barrier;
	private int i = 0;
	public Work(CyclicBarrier barrier,int i){
		this.barrier = barrier;
		this.i = i;
	}
	public void run(){
		try {
			doWork();
			barrier.await();
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	void doWork() {
		System.out.println("working"+i+"......");
		}
}

你可能感兴趣的:(java,thread)