【Java多线程】之十三:Java Callable Future Example
2015-08-09 19:01
351 查看
In last few posts, we learned a lot about java threads but sometimes we wish that a thread could return some value that we can use. Java 5 introduced java.util.concurrent.Callable interface in concurrency package that is similar to Runnable interface but it can return any Object and able to throw Exception.
Callable interface use Generic to define the return type of Object. Executors class provide useful methods to execute Callable in a thread pool. Since callable tasks run in parallel, we have to wait for the returned Object. Callable tasks return java.util.concurrent.Future object. Using Future we can find out the status of the Callable task and get the returned Object. It provides get() method that can wait for the Callable to finish and then return the result.
Future provides cancel() method to cancel the associated Callable task. There is an overloaded version of get() method where we can specify the time to wait for the result, it’s useful to avoid current thread getting blocked for longer time. There are** isDone()and **isCancelled() methods to find out the current status of associated Callable task.
Here is a simple example of Callable task that returns the name of thread executing the task after one second. We are using Executor framework to execute 100 tasks in parallel and use Future to get the result of the submitted tasks.
Click here learn about more
Callable interface use Generic to define the return type of Object. Executors class provide useful methods to execute Callable in a thread pool. Since callable tasks run in parallel, we have to wait for the returned Object. Callable tasks return java.util.concurrent.Future object. Using Future we can find out the status of the Callable task and get the returned Object. It provides get() method that can wait for the Callable to finish and then return the result.
Future provides cancel() method to cancel the associated Callable task. There is an overloaded version of get() method where we can specify the time to wait for the result, it’s useful to avoid current thread getting blocked for longer time. There are** isDone()and **isCancelled() methods to find out the current status of associated Callable task.
Here is a simple example of Callable task that returns the name of thread executing the task after one second. We are using Executor framework to execute 100 tasks in parallel and use Future to get the result of the submitted tasks.
Click here learn about more
相关文章推荐
- 如何在 Java 中正确使用 wait, notify 和 notifyAll – 以生产者消费者模型为例
- Java注解之常用注解
- Java程序设计之链表结构
- Java集合类总结
- Java基础学习总结---------IO概述(1)
- Ubuntu 15.04 kylin 配置jdk+eclipse+sdk+ndk
- 用Java实现分数加减和乘法计算
- 【Java多线程】之十二:Java Thread Pool Example using Executors and ThreadPoolExecutor
- Java中逻辑表达式的短路(先理解运算符的优先级)
- java对世界各个时区(TimeZone)的通用转换处理方法
- Java中得到GMT 时间(格林时间)
- spring 事务 aop:pointcut配置
- SSL介绍与Java实例
- Spring3.0学习札记一Spring IOC控制反转(4)
- java基础
- Java字符串切割
- Java反射机制之集合泛型的本质
- 设计模式之spring分析
- Java容器框架总结(一)
- gradle生成java Project并导入Eclipse中