如果想获取某一线程执行后的结果, 则使用Callable(类似于Runnable), Future(表示一个任务的生命周期, 并提供了相应的方法判断是否已经完成或取消, 以及任务的执行结果)
package com.test; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class Test4 { public static void main(String[] args) throws InterruptedException, ExecutionException { ExecutorService es = Executors.newCachedThreadPool();// 创建线程池对象 List<Future<String>> result = new ArrayList<Future<String>>();// 放结果用的集合 for (int i = 0; i < 3; i++) { Future<String> f = es.submit(new MyCall(i));// 线程执行完成以后可以通过引用获取返回值 result.add(f); } for (Future<String> f : result) { System.err.println("返回值:" + f.get());// get方法等线程返回结果, } System.err.println("完成...."); } } //声明一个类,可以被调用,类似于线程,但它可以拥有返回值 class MyCall implements Callable<String> { private int seq; public MyCall(int seq) { this.seq = seq; } // 抛出异常并可以拥有返回值 public String call() throws Exception { System.err.println("执行" + seq + "," + Thread.currentThread().getName()); Thread.sleep(3000); System.err.println("Weak up " + seq); return "完成" + seq;// 这是返回值 } }
相关推荐
/home/data/anaconda3/lib/python3.7/site-packages/pymysql/cursors.py:170: Warning: (1681, 'Integer display width is deprecated and will be removed in a future release.') result = self._query(query) ...
Architecture and Design for the Future Internet addresses the Networks of the Future and the Future Internet, focusing on networks aspects, offering both technical and non-technical perspectives....
python库。 资源全名:future-0.12.0.tar.gz
future-0.18.2-py3-none-any.whl 官网太难下载了
03 The Future of Architecture.pdf 04 j2ee_leon.pdf 04 j2ee_richard.pdf 05 SOA软件架构的实现.pdf 06 1Design Patterns.pdf 06 2Agile Software Development.pdf 06 3IsDesignDead.pdf 07 Architecture.pdf 07 ...
python安装打包依赖包-future-0.18.1.tar.gz
Springer.Future.Internet.Technologies.and.Trends.First.International.ConferenceSpringer.Future.Internet.Technologies.and.Trends.First.International.ConferenceSpringer.Future.Internet.Technologies.and....
future-0.18.2-py3-none-any.whl ,官网下载有点慢 future-0.18.2-py3-none-any.whl ,为什么我把积分改为0,他又会自己变成29啊
Orthogonal Waveforms and Filter Banks for Future Communication Systems provides an up-to-date account of orthogonal filter bank-based multicarrier (FBMC) systems and their applications in modern and ...
The Future of Retail - To the extent that there are two distinct sides to the argument, the position of Offline-to-Online (O2O) advocates is that, as more offline retailers are closing, brick-and-...
Crafting Your Research Future
[竞争大未来].Competing.for.the.Future.(美)哈梅尔&普拉哈拉德.扫描版.pdf [竞争大未来].Competing.for.the.Future.(美)哈梅尔&普拉哈拉德.扫描版.pdf
The Future Evolution of High-Performance Microprocessors
c++11多线程编程库中的future库的使用实例,供多线程编程参考学习
Document that explain the future of PowerBuilder
主要介绍了Java使用Callable和Future创建线程操作,结合实例形式分析了java使用Callable接口和Future类创建线程的相关操作技巧与注意事项,需要的朋友可以参考下
future, currently in their school or pre-school years, are likely to access and interact with digital resources in five to ten years’ time. This is to help library and information services to ...
Reinforcement Learning_ Past, Present, and Future Perspectives,强化学习:过去、现在和未来(NeurIPS 2019),by Katja Hofmann,非常全面的强化学习总结英文资料,值得学习参考。
Big Data Analytics in Future Power Systems by:Ahmed F. Zobaa and Trevor J. Bihl ISBN-10 书号: 1138095885 ISBN-13 书号: 9781138095885 Edition 版本: 1 出版日期: 2018-09-24 pages 页数: (188) $99.95 ...