package cn.nyzy;
import java.util.concurrent.CountDownLatch;
public class CountDownLatchTest{
public static void main( String[ ] args ) throws InterruptedException {
CountDownLatch count = new CountDownLatch(3);
Thread thread1= new TranslateThread("1st content",count);
Thread thread2= new TranslateThread("2st content",count);
Thread thread3= new TranslateThread("3st content",count);
thread1.start();
thread2.start();
thread3.start();
count.await();
System.out.println("所有的线程运行完毕");
}
}
class TranslateThread extends Thread {
private String content;
private final CountDownLatch count;
public TranslateThread(String content,CountDownLatch count)
{
this.content=content;
this.count=count;
}
@Override
public void run() {
if(Math.random()<0.5)
{
throw new RuntimeException("原文存在非法字符");
}
System.out.print(this.content+"翻译完毕,内容是。。。。");
count.countDown();
}
}