Java Callable
和Future在
多线程编程中经常使用。在最后几篇文章中,我们学到了很多关于java线程的知识,但有时我们希望线程可以返回一些可以使用的值。Java 5在并发包中引入了java.util.concurrent.Callable
接口,类似于Runnable
接口但它可以返回对象并且能够抛出异常。
Java Callable
Java Callable
接口使用Generic
来定义对象的返回类型。Executors
类提供了在线程池中执行Java Callable
的方法。由于可调用任务并行运行,必须等待返回的对象。
下面是Java Callable
任务的一个简单示例,它返回一秒钟后执行任务的线程的名称。使用Executor
框架并行执行100
个任务,并使用Java Future
获取提交任务的结果。
import java.util.ArrayList;
import java.util.Date;
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 MyCallable implements Callable<String> {
@Override
public String call() throws Exception {
Thread.sleep(1000);
//return the thread name executing this callable task
return Thread.currentThread().getName();
}
public static void main(String args[]){
//Get ExecutorService from Executors utility class, thread pool size is 10
ExecutorService executor = Executors.newFixedThreadPool(10);
//create a list to hold the Future object associated with Callable
List<Future<String>> list = new ArrayList<Future<String>>();
//Create MyCallable instance
Callable<String> callable = new MyCallable();
for(int i=0; i< 100; i++){
//submit Callable tasks to be executed by thread pool
Future<String> future = executor.submit(callable);
//add Future to the list, we can get return value using Future
list.add(future);
}
for(Future<String> fut : list){
try {
//print the return value of Future, notice the output delay in console
// because Future.get() waits for task to get completed
System.out.println(new Date()+ "::"+fut.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
//shut down the executor service now
executor.shutdown();
}
}
当执行上面的程序,要注意输出有延迟,因为java Future get()
方法等待java可调用任务完成。只有10
个线程用于执行这些任务。
下面是上述程序输出的片段 -
Mon Dec 31 20:40:15 PST 2020::pool-1-thread-1
Mon Dec 31 20:40:16 PST 2020::pool-1-thread-2
Mon Dec 31 20:40:16 PST 2020::pool-1-thread-3
Mon Dec 31 20:40:16 PST 2020::pool-1-thread-4
Mon Dec 31 20:40:16 PST 2020::pool-1-thread-5
Mon Dec 31 20:40:16 PST 2020::pool-1-thread-6
Mon Dec 31 20:40:16 PST 2020::pool-1-thread-7
Mon Dec 31 20:40:16 PST 2020::pool-1-thread-8
Mon Dec 31 20:40:16 PST 2020::pool-1-thread-9
Mon Dec 31 20:40:16 PST 2020::pool-1-thread-10
Mon Dec 31 20:40:16 PST 2020::pool-1-thread-2
... ...
提示:如果想要覆盖Java Future
接口的某些方法,例如,在一些默认时间之后将get()
方法覆盖为超时而不是无限期地等待,那么Java FutureTask
类就是方便的,它是Future
接口的基本实现。