当前位置:网站首页>【多线程编程】Future接口获取线程执行结果数据
【多线程编程】Future接口获取线程执行结果数据
2022-06-13 05:01:00 【阿小冰】
两种异步模式
在JAVA的并发编程中,会有两种异步编程模型
- 直接以异步的形式来并行运行其他的任务,不需要返回任务的结果数据
- 以异步的形式运行其他的任务,需要返回结果
我们只看一下有结果返回的模型
有结果的异步模型
1、使用Future实现异步结果返回
public class FutureDemo { public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executorService = Executors.newSingleThreadExecutor(); Future<String> future = executorService.submit(new Callable<String>() { @Override public String call() throws Exception { return "使用Future获取异步结果数据"; } }); System.out.println(future.get()); executorService.shutdown(); } }
2、使用FutureTask获取异步结果
public class FutureTaskDemo { public static void main(String[] args) throws ExecutionException, InterruptedException { FutureTask<String> futureTask=new FutureTask<>(new Callable<String>() { @Override public String call() throws Exception { return "使用FutureTask获取异步结果数据"; } }); new Thread(futureTask).start(); System.out.println(futureTask.get()); } }
FutureTask也可以结合线程池来使用
public class FutureTaskDemo { public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executorService = Executors.newSingleThreadExecutor(); FutureTask<String> futureTask = new FutureTask<>(new Callable<String>() { @Override public String call() throws Exception { return "使用FutureTask + Thread Pool获取异步结果数据"; } }); executorService.execute(futureTask); new Thread(futureTask).start(); System.out.println(futureTask.get()); } }
边栏推荐
- Luogu p1036 number selection
- C language learning log 2.6
- Bomb disposal cat
- CMB's written test -- data analysis
- Sampo Lock
- QT brushes and brushes
- Clause 26: avoid overloading universal reference types
- MySQL8.0.13安装教程(有图)
- Implementing the driver registration initcall mechanism on stm32
- Explain the opencv function cv:: add() in detail, and attach sample code and running results of various cases
猜你喜欢
The games that you've tasted
Leetcode game 297 (20220612)
Explain the opencv function cv:: add() in detail, and attach sample code and running results of various cases
Section 7 - structures
About mission planning and improving execution
Analysis on the usage, response and global delivery of provide/inject
利用Javeswingjdbc基于mvc设计系统
Stepping on a horse (one stroke)
Design system based on MVC using javeswingjdbc
QT interface rendering style
随机推荐
Luogu p1036 number selection
Explain the role of key attribute in V-for
Handwritten promise and its method, with detailed notes
RMQ、LCA
metaRTC4.0稳定版发布
How to handle async/await error messages gracefully
MySQL8.0.13安装教程(有图)
Promise processing JS multithreads get the same processing result after all the results are obtained
Robot pose description and coordinate transformation
RuoYi-Cloud启动教程(手把手图文)
Advanced C language - Section 1 - data storage
OpenCV中的saturate操作(饱和操作)究竟是怎么回事
Clause 47: please use traits classes to represent type information
Section 8 - Practical commissioning techniques
Sub paragraph of Chapter 16
Chapter 17 free space management
Implementing the driver registration initcall mechanism on stm32
关于匿名内部类
External sort
QT client development -- driver loading problem of connecting to MySQL database