CyclicBarrier 类的应用示例:如开启5个线程,在每个线程内部打印输出"Hello World",5个线程先全部输出"Hello",之后再输出"World"。
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class Printer {
CyclicBarrier cyclicBarrier = new CyclicBarrier(5);
private void print() {
for (int i = 0; i < 5; i++) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("currentThread" + Thread.currentThread() + ":Hello");
try {
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
System.out.println("currentThread" + Thread.currentThread() + ":World");
}
}, "printThread_" + i).start();
}
}
public static void main(String[] args) {
Printer printer = new Printer();
printer.print();
}
}
输入结果示例:
currentThreadThread[printThread_0,5,main]:Hello
currentThreadThread[printThread_2,5,main]:Hello
currentThreadThread[printThread_4,5,main]:Hello
currentThreadThread[printThread_3,5,main]:Hello
currentThreadThread[printThread_1,5,main]:Hello
currentThreadThread[printThread_4,5,main]:World
currentThreadThread[printThread_3,5,main]:World
currentThreadThread[printThread_2,5,main]:World
currentThreadThread[printThread_1,5,main]:World
currentThreadThread[printThread_0,5,main]:World
CountDownLatch 类的应用示例:某个线程要等待前面的线程执行完毕,它才开始执行。
import java.util.concurrent.CountDownLatch;
public class CountDownLatchTest {
public static void main(String[] args) {
final CountDownLatch countDownLatch = new CountDownLatch(2);
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("currentThread" + Thread.currentThread() + " Children begin to enter.");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("currentThread" + Thread.currentThread() + " Children in. Over.");
countDownLatch.countDown();
}
}, "Children_Thread").start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("currentThread" + Thread.currentThread() + " Ladies begin to enter.");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("currentThread" + Thread.currentThread() + " Ladies in. Over.");
countDownLatch.countDown();
}
}, "Ladies_Thread").start();
new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("currentThread" + Thread.currentThread() + " Gentlemen are waiting.");
countDownLatch.await();
System.out.println("currentThread" + Thread.currentThread() + " Gentlemen begin to enter.");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("currentThread" + Thread.currentThread() + " Gentlemen in. Over.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "Gentlemen_Thread").start();
}
}
输入结果示例:
currentThreadThread[Children_Thread,5,main] Children begin to enter.
currentThreadThread[Ladies_Thread,5,main] Ladies begin to enter.
currentThreadThread[Gentlemen_Thread,5,main] Gentlemen are waiting.
currentThreadThread[Children_Thread,5,main] Children in. Over.
currentThreadThread[Ladies_Thread,5,main] Ladies in. Over.
currentThreadThread[Gentlemen_Thread,5,main] Gentlemen begin to enter.
currentThreadThread[Gentlemen_Thread,5,main] Gentlemen in. Over.
由于作者水平有限,语言描述及代码实现中难免有纰漏,望各位看官多提宝贵意见!
Hello , World !
感谢所有!