CyclicBarrier类测试

package t1;

import java.util.concurrent.CyclicBarrier;

public class TestThread26 extends Thread {
private CyclicBarrier cy;

public TestThread26(CyclicBarrier cy) {
this.cy = cy;
}

@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + "开始等待其他线程");
cy.await();
System.out.println(Thread.currentThread().getName() + "开始执行");
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName() + "执行完成");
} catch (Exception e) {
}
}

public static void main(String[] args) {
int cnt = 2;
CyclicBarrier cy = new CyclicBarrier(2);
for (int i = 0; i < cnt; i++) {
System.out.println("创建工作线程" + i);
TestThread26 t = new TestThread26(cy);
t.start();
}
}

}

 

测试结果:

 

posted @ 2020-04-01 16:05  工设091  阅读(82)  评论(0)    收藏  举报