如何在java中实现跨线程的通讯
2024-10-19 12:48:55
一般而言,如果没有干预的话,线程在启动之后会一直运行到结束,但有时候我们又需要很多线程来共同完成一个任务,这就牵扯到线程间的通讯。
如何让两个线程先后执行?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的基本用法
- 创建一个CountdownLatch并赋初始值,
CountdownLatch countDownLatch = new CountDownLatch(3;
- 在需要等待的线程中调用
countDownLatch.await()
进入等待状态; - 在其他线程执行中适当的时候调用
countDownLatch.countDown()
,会使内部否计数值减一; - 当
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
的用法:
- 首先还是需要先创建一个
CyclicBarrier对象,设置初始值,
CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
- 各个线程同步启动,在完成一些逻辑之后,调用
cyclicBarrier.await()
开始等待; - 当所有的线程都调用了
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中其实提供了更多的链式操作,不过写起来比较复杂,看起来也不清晰。
最新文章
- jQuery技巧大放送
- 怎么学JavaScript?
- android studio 生成aar包并在其他工程引用 (导入)aar包
- Redis(一)基础
- 基于JavaScript实现表单密码的隐藏和显示出来
- 用Javascript弹出div定义的消息框并往块里面填写文字
- rest简单实例
- VB execl文件后台代码,基础语法
- super.getClass()与this.getClass()
- IOS开发-UI学习-沙盒机制&;文件操作
- ";《算法导论》之‘线性表’";:基于数组实现的单链表
- 【MySql】update用法
- PAT甲级1022 Digital Library
- 广州.net俱乐部12月份ABP框架活动场地征集、志愿者征集、合作讲师\副讲师征集
- Java技能
- C#WinForm应用程序中嵌入ECharts图表
- Android分享到微信和朋友圈的工具类
- iOS:UIApplication和它对象的代理
- [C++]红色波浪线是什么意思
- Linux ARP代理 与 NAT