import java.util.concurrent.CyclicBarrier;
public class Test {
public static void main(String[] args) throws Exception {
CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + "执行开始");
Thread.sleep(3000L);
cyclicBarrier.await();
System.out.println(Thread.currentThread().getName() + "执行结束");
} catch (Exception e) {
e.printStackTrace();
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + "执行开始");
cyclicBarrier.await();
System.out.println(Thread.currentThread().getName() + "执行结束");
} catch (Exception e) {
e.printStackTrace();
}
}
});
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + "执行开始");
cyclicBarrier.await();
System.out.println(Thread.currentThread().getName() + "执行结束");
} catch (Exception e) {
e.printStackTrace();
}
}
});
t1.start();
t2.start();
t3.start();
Thread.sleep(6000L);
System.out.println(Thread.currentThread().getName() + "执行结束");
}
}
java并发之CyclicBarrier
于 2025-01-01 22:19:21 首次发布