Future多用于耗时线程的计算,主线程可以在完成自己的任务后,再去查询该Future是否执行完毕并获取结果。他有一个回调函数protected void done(),当任务结束时,该回调函数会被触发。因此,只需重载该函数,即可实现在线程刚结束时就做一些事情。
FutureTask则是一个RunnableFuture<V>,即实现了Runnbale又实现了Futrue<V>这两个接口,另外它还可以包装Runnable和Callable<V>,它可以通过Thread包装来直接执行,也可以提交给ExecuteService来执行,可以通过v get()返回执行结果,在线程体没有执行完成的时候,主线程一直阻塞等待,执行完则直接返回结果。
简单测试future对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();
未完待续。
原文:http://www.cnblogs.com/leeqq/p/3969283.html