一般而言,如果没有干预的话,线程在启动之后会一直运行到结束,但有时候我们又需要很多线程来共同完成一个任务,这就牵扯到线程间的通讯。

如何让两个线程先后执行?Thread.join方法

private static void demo2() {
Thread A = new Thread(new Runnable() {
@Override
public void run() {
printNumber("A");
}
});
Thread B = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("B starts waiting for A");
try {
A.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
printNumber("B");
}
});
B.start();
A.start();
}

其中A.join()的意思即是等待A线程执行完毕。

如何让两个线程交互执行?object.wait和object.notify方法

 /**
* A 1, B 1, B 2, B 3, A 2, A 3
*/
private static void demo3() {
Object lock = new Object();
Thread A = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
System.out.println("A 1");
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("A 2");
System.out.println("A 3");
}
}
});
Thread B = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
System.out.println("B 1");
System.out.println("B 2");
System.out.println("B 3");
lock.notify();
}
}
});
A.start();
B.start();
}

A在输出完1之后等待B的notify才会去执行其他的操作。

如何让四个线程的其中一个等待其他三个执行完毕?CountdownLatch就是干这个的。

CountdownLatch的基本用法

  1. 创建一个CountdownLatch并赋初始值,CountdownLatch countDownLatch = new CountDownLatch(3;
  2. 在需要等待的线程中调用 countDownLatch.await() 进入等待状态;
  3. 在其他线程执行中适当的时候调用 countDownLatch.countDown() ,会使内部否计数值减一;
  4. 当 countDown()导致count值为 0, 则处于等待态的线程开始执行。
 private static void runDAfterABC() {
int worker = 3;
CountDownLatch countDownLatch = new CountDownLatch(worker);
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("D is waiting for other three threads");
try {
countDownLatch.await();
System.out.println("All done, D starts working");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
for (char threadName='A'; threadName <= 'C'; threadName++) {
final String tN = String.valueOf(threadName);
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(tN + "is working");
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(tN + "finished");
countDownLatch.countDown();
}
}).start();
}
}

如何让三个线程的各自开始做一些事情,然后在某个时间点上进行同步?CyclicBarrier 是干这个的。

CyclicBarrier 的用法:

  1. 首先还是需要先创建一个CyclicBarrier对象,设置初始值,CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
  2. 各个线程同步启动,在完成一些逻辑之后,调用 cyclicBarrier.await()开始等待;
  3. 当所有的线程都调用了 cyclicBarrier.await() 之后,每个线程都可以执行之后的逻辑。
 private static void runABCWhenAllReady() {
int runner = 3;
CyclicBarrier cyclicBarrier = new CyclicBarrier(runner);
final Random random = new Random();
for (char runnerName='A'; runnerName <= 'C'; runnerName++) {
final String rN = String.valueOf(runnerName);
new Thread(new Runnable() {
@Override
public void run() {
long prepareTime = random.nextInt(10000) + 100;
System.out.println(rN + "is preparing for time:" + prepareTime);
try {
Thread.sleep(prepareTime);
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println(rN + "is prepared, waiting for others");
cyclicBarrier.await(); // The current runner is ready, waiting for others to be ready
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
System.out.println(rN + "starts running"); // All the runners are ready to start running together
}
}).start();
}
}

如何取回某个线程的返回值了?Callable 是干这个的。

先看下定义:

@FunctionalInterface
public interface Callable<V> {
/**
* Computes a result, or throws an exception if unable to do so.
*
* @return computed result
* @throws Exception if unable to compute a result
*/
V call() throws Exception;
}

然后直接给个例子:

 private static void doTaskWithResultInWorker() {
Callable<Integer> callable = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
System.out.println("Task starts");
Thread.sleep(1000);
int result = 0;
for (int i=0; i<=100; i++) {
result += i;
}
System.out.println("Task finished and return result");
return result;
}
};
FutureTask<Integer> futureTask = new FutureTask<>(callable);
new Thread(futureTask).start();
try {
System.out.println("Before futureTask.get()");
System.out.println("Result:" + futureTask.get());
System.out.println("After futureTask.get()");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}

注意,其中futureTask.get()方法是阻塞调用。

以上都是一些很基本的应用,在新版本的CompleteFuture中其实提供了更多的链式操作,不过写起来比较复杂,看起来也不清晰。

最新文章

  1. jQuery技巧大放送
  2. 怎么学JavaScript?
  3. android studio 生成aar包并在其他工程引用 (导入)aar包
  4. Redis(一)基础
  5. 基于JavaScript实现表单密码的隐藏和显示出来
  6. 用Javascript弹出div定义的消息框并往块里面填写文字
  7. rest简单实例
  8. VB execl文件后台代码,基础语法
  9. super.getClass()与this.getClass()
  10. IOS开发-UI学习-沙盒机制&amp;文件操作
  11. &quot;《算法导论》之‘线性表’&quot;:基于数组实现的单链表
  12. 【MySql】update用法
  13. PAT甲级1022 Digital Library
  14. 广州.net俱乐部12月份ABP框架活动场地征集、志愿者征集、合作讲师\副讲师征集
  15. Java技能
  16. C#WinForm应用程序中嵌入ECharts图表
  17. Android分享到微信和朋友圈的工具类
  18. iOS:UIApplication和它对象的代理
  19. [C++]红色波浪线是什么意思
  20. Linux ARP代理 与 NAT

热门文章

  1. js 修改字符串中某些字符的样式
  2. express函数参数之next
  3. 两组数据的均值是否具有显著差异的T检验
  4. JQuery补充——获取与设置表单值
  5. flex stacked column graph
  6. 20155230 《Java程序设计》实验五 Java网络编程及安全
  7. 20155301 2016-2017-2 《Java程序设计》第10周学习总结
  8. 20155305 2016-2017-2 《Java程序设计》 实验五 Java网络编程及安全实验报告
  9. 20155310马英林 实验2 Windows口令破解
  10. java课堂实践(5月17日)20155317 王新玮