CyclicBarrier

import java.util.concurrent.BrokenBarrierException;

import java.util.concurrent.CyclicBarrier;

 

public class TestCyclicBarrier {

static CyclicBarrier c = new CyclicBarrier(2, new A());

public static void main(String[] args) {

new Thread(new Runnable(){

 

@Override

public void run() {

try {

//Waits until all parties have invoked await on this barrier. 

 

 

 

c.await();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (BrokenBarrierException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(1);

}

 

}).start();

 

new Thread(new Runnable(){

 

@Override

public void run() {

try {

c.await();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (BrokenBarrierException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(2);

}

 

}).start();

 

}

 

static class A implements Runnable{

 

@Override

public void run() {

System.out.println("3");

 

}

 

}

 

}

 

你可能感兴趣的:(CyclicBarrier)