(近期通过Educoder上的课程实践复习Java的基础芝士,在多线程的章节中,遇到了使用Callable和Future创建线程的关卡,一时有点懵,特此记录相关芝士点以备忘。)
从java1.5版本开始,我们就在Thread和Runnable两种方式创建线程基础上,新增加了使用Callable和Future创建线程,很好地解决了前者在执行完任务之后无法获取执行结果,而如果需要获取执行结果,就必须通过共享变量或者使用线程通信的方式来达到的问题。
使用Callable和Future创建线程,我们就可以在任务执行完毕后就得到任务的执行结果,Callable产生结果,Future获取结果。
此处借用Educoder任务关卡描述中的使用步骤:
使用步骤如下:
1、创建 Callable 接口的实现类,并实现 call() 方法,该 call() 方法将作为线程执行体,并且有返回值;
2、创建 Callable 实现类的实例,使用 FutureTask 类来包装 Callable 对象,该 FutureTask 对象封装了该Callable 对象的 call() 方法的返回值;
3、使用 FutureTask 对象作为 Thread 对象的 target 创建并启动新线程;
4、调用 FutureTask 对象的 get() 方法来获得子线程执行结束后的返回值。
查看源码发现Callable接口与Runnable类似,但是有返回值。Callable接口位于java.util.concurrent包下,该接口只有一个方法call():
* @see Executor
* @since 1.5
* @author Doug Lea
* @param <V> the result type of method {@code call}
*/
@FunctionalInterface
public interface Callable<V> {
/**
* Computes a result, or throws an exception if unable to do so.
*
* @return computed result
* @throws Exception if unable to compute a result
*/
V call() throws Exception;
}
类型参数是返回值的类型。例如Callable表示一个最终返回Integer对象的一步计算。查阅相关资料,一般情况下Callable是配合ExecutorService来使用的,在ExecutorService接口中声明了若干个重载的submit方法:
<T> Future<T> submit(Callable<T> task);
<T> Future<T> submit(Runnable task, T result);
Future<?> submit(Runnable task);
同处java.util.concurrent包下的Future接口是对具体的Callable任务的执行结果进行取消、查询是否完成、获取结果。它只有五个方法,根据源码解释如下:
public interface Future<V> {
/**
* 尝试取消执行此任务。如果任务已经完成,已经被取消,或者由于某些其他原因而无法取消,则此尝试将失败。
* 如果成功,并且并且在调用{@code cancel}时此任务尚未开始,则该任务永远不要运行。
* 如果任务已经开始,则{@code mayInterruptIfRunning}参数确定在尝试停止任务时是否应中断执行此任务的线程。
*/
boolean cancel(boolean mayInterruptIfRunning);
/**
* 如果此任务在正常完成之前被取消,则返回{@code true}。
* (即表示任务是否被取消成功,如果在任务正常完成前被取消成功,则返回 true。)
*/
boolean isCancelled();
/**
* 完成可能是由于正常终止,异常或取消 —— 在所有这些情况下,此方法都将返回 {@code true}。
* (即用于判断是否已经完成,若任务完成,则返回true)
*/
boolean isDone();
/**
* 在必要时等待计算完成,然后检索其结果。
* (即用来获取执行结果,这个方法会产生阻塞,会一直等到任务执行完毕才返回)
*/
V get() throws InterruptedException, ExecutionException;
/**
* 必要时最多等待给定时间以完成计算,然后检索其结果(如果有)。
* (即用来获取执行结果,如果在指定时间内,还没获取到结果,就直接返回null)
*/
V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
由于Future只是一个接口,无法直接用来创建对象使用,因此我们需要FutureTask包装器,它可将Callable转换成Future和Runnable,同时实现了二者的接口,因此FutureTask既可以作为Runnable被线程执行,又可以作为Future得到Callable的返回值。FutureTask提供了2个构造器:
/**
* Creates a {@code FutureTask} that will, upon running, execute the
* given {@code Callable}.
*
* @param callable the callable task
* @throws NullPointerException if the callable is null
*/
public FutureTask(Callable<V> callable) {
if (callable == null)
throw new NullPointerException();
this.callable = callable;
this.state = NEW; // ensure visibility of callable
}
/**
* Creates a {@code FutureTask} that will, upon running, execute the
* given {@code Runnable}, and arrange that {@code get} will return the
* given result on successful completion.
*
* @param runnable the runnable task
* @param result the result to return on successful completion. If
* you don't need a particular result, consider using
* constructions of the form:
* {@code Future<?> f = new FutureTask<Void>(runnable, null)}
* @throws NullPointerException if the runnable is null
*/
public FutureTask(Runnable runnable, V result) {
this.callable = Executors.callable(runnable, result);
this.state = NEW; // ensure visibility of callable
}
Educoder任务关卡用的例子是在runThread(int num)函数中执行线程,创建Callable线程,Callable线程需要执行求第num项斐波那契数列的值,最后在runThread函数中获取Callable线程执行的结果,并打印输出。
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class Task {
public void runThread(int num) {
//请在此添加实现代码
/********** Begin **********/
// 在这里开启线程 获取线程执行的结果
Callable<Integer> callable = new ThreadCallable(num);
// 使用 FutureTask 类来包装 Callable 对象,该 FutureTask 对象封装了该 Callable 对象的 call() 方法的返回值;
FutureTask<Integer> futureTask = new FutureTask<>(callable);
new Thread(futureTask).start();//开启线程
try {
//调用包装类的get方法获取执行结果
Integer result = futureTask.get();
System.out.println("线程的返回值为:" + result);
} catch (Exception e) {
e.printStackTrace();
}
/********** End **********/
}
}
//请在此添加实现代码
/********** Begin **********/
/* 在这里实现Callable接口及方法 */
class ThreadCallable implements Callable<Integer> {
private int num;
public ThreadCallable() {
}
public ThreadCallable(int num) {
this.num = num;
}
//实现call()方法,并作为线程执行体,具有返回值
public Integer call() throws Exception {
int[] arr = new int[2];
arr[0] = 1;
arr[1] = 1;
for (int i = 2; i < num; i++) {
int tmp = arr[1];
arr[1] = arr[0] + arr[1];
arr[0] = tmp;
}
return arr[1];
}
}
/********** End **********/
输入:3
输出:线程的返回值为:2
输入:5
输出:线程的返回值为:5