package player.kent.chen.learn.future; import java.io.File; import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; import org.apache.commons.io.FileUtils; public class HelloFutureTask { public static void main(String[] args) { //一个待完成事项 Callable<String> callable = new Callable<String>() { public String call() throws Exception { return FileUtils.readFileToString(new File("/home/kent/temp/1.txt")); } }; //生成FutureTask对象 FutureTask<String> task = new FutureTask<String>(callable) { @Override //重载这个方法,可以在任务执行完时干点事 protected void done() { super.done(); System.out.println("The task is done"); } }; //生成一个线程启动它 Thread t = new Thread(task); t.start(); try { //阻塞式地等待结果 String text = task.get(); System.out.println(text); } catch (Exception e) { } } }