11.线程通信CountDownLatch
package demo2;
import java.util.concurrent.CountDownLatch;
/**
* Created by liudan on 2017/7/27.
*/
public class DemoCountDownLatch {
public static void main(String[] args) {
final CountDownLatch countDownLatch = new CountDownLatch(3);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
System.err.print(">登录游戏:...\t");
countDownLatch.await();
System.err.println("----------- 登录成功 SUCCESS-------------");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"t");
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
System.err.print(" 3 ");
countDownLatch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
System.err.print(" 2 ");
countDownLatch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"t2");
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(4000);
System.err.println(" 1 ");
countDownLatch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"t3");
t.start();
t1.start();
t2.start();
t3.start();
}
}
输出:>登录游戏:... 3 2 1 ----------- 登录成功 SUCCESS-------------