详解JDK中ExecutorService与Callable和Future对线程的支持
作者:莫欺少年穷Java 时间:2023-11-25 07:39:07
详解JDK中ExecutorService与Callable和Future对线程的支持
1、代码背景:
假如有Thread1、Thread2、Thread3、Thread4四条线程分别统计C、D、E、F四个盘的大小,所有线程都统计完毕交给Thread5线程去做汇总,应当如何实现?
2、代码:
统计“盘子”大小的代码,此处实现jdk中的Callable接口,
package com.wang.test.concurrent;
import java.util.concurrent.Callable;
public class Task1 implements Callable<Integer> {
private int x;
private int y;
public Task1(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public Integer call() throws Exception {
return x*y;
}
}
统计汇总的代码,也是实现jdk中的Callable接口,
package com.wang.test.concurrent;
import java.util.concurrent.Callable;
public class Task2 implements Callable<Integer> {
private int x;
private int y;
private int q;
private int w;
public Task2(int x, int y, int q, int w) {
this.x = x;
this.y = y;
this.q = q;
this.w = w;
}
@Override
public Integer call() throws Exception {
return x + y + q + w;
}
}
客户端:使用JDK中Executors.newFixedThreadPool方法创建ExecutorService,ExecutorService的submit方法接收Callable接口的实现,JDK内部将弄成线程处理,使用Future接收submit方法的返回值,当future调用get方法时,如果线程还没有执行完,程序阻塞在这里,知道线程执行完。
package com.wang.test.concurrent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Client {
public static void main(String[] args) throws Exception {
ExecutorService pool = Executors.newFixedThreadPool(4);
Task1 t1 = new Task1(1,2);
Task1 t2 = new Task1(23,34);
Task1 t3 = new Task1(23,456);
Task1 t4 = new Task1(3,33);
Future<Integer> f1 = pool.submit(t1);
Future<Integer> f2 = pool.submit(t2);
Future<Integer> f3 = pool.submit(t3);
Future<Integer> f4 = pool.submit(t4);
//Future调用get方法时,如果线程还没有执行完,程序阻塞在这里
Task2 t5 = new Task2(f1.get(), f2.get(), f3.get(), f4.get());
Future<Integer> f5 = pool.submit(t5);
System.out.println(f5.get());
pool.shutdown();
}
}
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://hejiawangjava.iteye.com/blog/2393827
标签:JDK,ExecutorService
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot2.2 集成 activity6实现请假流程(示例详解)
2022-09-18 08:54:21
![](https://img.aspxhome.com/file/2023/3/63113_0s.png)
java switch语句使用注意的四大细节
2022-07-10 10:46:04
Java实现添加条形码到PDF表格的方法详解
2023-04-26 12:37:25
![](https://img.aspxhome.com/file/2023/3/70343_0s.png)
Java基础题新手练习(二)
2022-03-10 00:11:57
![](https://img.aspxhome.com/file/2023/5/69705_0s.jpg)
Java实现AES算法的实例代码
2021-09-25 15:05:52
Java使用Collections工具类对List集合进行排序
2022-10-12 23:59:12
java语言实现猜数字游戏
2023-11-24 00:19:38
![](https://img.aspxhome.com/file/2023/6/59206_0s.jpg)
Java安全之Tomcat6 Filter内存马问题
2022-11-20 07:29:23
![](https://img.aspxhome.com/file/2023/7/61027_0s.jpg)
图文详解Java中的字节输入与输出流
2022-12-23 23:14:48
![](https://img.aspxhome.com/file/2023/4/81884_0s.jpg)
Android五大布局与实际应用详解
2022-01-30 05:21:36
![](https://img.aspxhome.com/file/2023/8/139108_0s.png)
Runtime.getRuntime().exec 路径包含空格的解决
2023-05-15 23:54:32
![](https://img.aspxhome.com/file/2023/6/83056_0s.png)
Android编程实现自动检测版本及自动升级的方法
2023-06-07 16:24:23
![](https://img.aspxhome.com/file/2023/0/98640_0s.jpg)
在Android中调用WebService实例
2023-10-27 12:00:10
![](https://img.aspxhome.com/file/2023/2/139082_0s.jpg)
SpringMVC多个文件上传及上传后立即显示图片功能
2021-06-26 12:41:08
C#控制台模拟电梯工作原理
2021-08-06 02:35:33
Android蓝牙的开启和搜索设备功能开发实例
2022-11-07 20:42:17
![](https://img.aspxhome.com/file/2023/4/123284_0s.png)
详解c# 类的构造方法
2023-02-10 17:17:19
Redis缓存策略超详细讲解
2023-11-08 06:20:10
![](https://img.aspxhome.com/file/2023/8/66078_0s.png)
Java中关于二叉树层序遍历深入了解
2023-07-26 07:06:09
![](https://img.aspxhome.com/file/2023/6/61076_0s.png)
Android 获得屏幕宽高的三种方式
2023-03-29 00:26:51
![](https://img.aspxhome.com/file/2023/4/113304_0s.gif)