Futurer多用于
耗时线程的计算,主线程可以在完成自己的任务后,再去查询该Future是否执行完毕并获取结果。他有一个回调函数protected void done(),当任务结束时,该回调函数会被触发。因此,只需重载该函数,即可实现在线程刚结束时就做一些事情。
Future可对具体的调度任务的执行结果进行查看,最为关键的是它可以检查对应的任务是否已经完成(isDone),也可以阻塞在get方法上一直等待任务返回结果。包装Runnable和Callable的差别就是Runnable是没有结果可以返回的,就算是通过Future也看不到任务调度的结果。
Future源码很简单,定义了对包装了的线程的操作,从字面上大家也能理解:cancel取消执行线程,参数为true代表强制取消,false等待线程执行完毕再取消,isDone回调方法,get阻塞获取,想深入了解看API。
public interface Future<V> {
boolean cancel(boolean mayInterruptIfRunning);
boolean isCancelled();
boolean isDone();
V get() throws InterruptedException, ExecutionException;
V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
FutureTask则是一个RunnableFuture<V>,即实现了Runnbale又实现了Futrue<V>这两个接口,另外它还可以包装Runnable和Callable<V>,它可以通过Thread包装来直接执行,也可以提交给ExecuteService来执行,可以通过v get()返回执行结果,在线程体没有执行完成的时候,主线程一直阻塞等待,执行完则直接返回结果。
简单说来FutureTask是对runnable和callable的包装,get()方法则返回结果,cancel(true)则可以强制终止线程。
package com.lzq.test;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class TestFuture {
public static void main(String[] args) {
// 通过线程池来创建FutureTask
ExecutorService executor = Executors.newFixedThreadPool(3);
try {
// 测试包装runnable
Future<?> run = executor.submit(new Runnable() {
@Override
public void run() {
System.out.println("runnable");
}
});
System.out.println("runable:" + run.get());
// 测试包装callable
Future<String> call = executor.submit(new Callable<String>() {
@Override
public String call() throws Exception {
return "call";
}
});
// 调用get方法,当前线程会阻塞,等待任务执行完毕后才往下执行,适合耗时的运算
System.out.println("call: " + call.get());
// 测试cancel
Future<String> cancelFuture = executor
.submit(new Callable<String>() {
@Override
public String call() throws Exception {
try {
while (true) {
System.out.println("cancel running.");
Thread.sleep(50);
}
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
return "cancel false";
}
});
System.out.println("cancel: " + cancelFuture.cancel(true));
// 测试抛异常
Future<String> exception = executor.submit(new Callable<String>() {
@Override
public String call() throws Exception {
throw new Exception("new exception!");
}
});
System.out.println("exception: " + exception.get());
} catch (Exception e) {
System.out.println(e.toString());
}
// 线程他关闭
executor.shutdownNow();
}
}
直接用Thread实现:
FutureTask<String> ft = new FutureTask<String>(new Callable<String>() {
@Override
public String call() throws Exception {
return "call";
}
});
Thread thread = new Thread(ft);
thread.start();
//executor.execute(ft);