/** * Created with IntelliJ IDEA. * User: 菜鸟大明 * Date: 14-10-23 * Time: 下午7:02 * To change this template use File | Settings | File Templates. */ public class MyCallable1 implements Callable { @Override public Object call() throws Exception { System.out.println("call"); return "end"; } public static void main(String[] args) throws ExecutionException, InterruptedException { MyCallable1 myCallable1 = new MyCallable1(); FutureTask fk = new MyFutureTask(myCallable1); // 它可以经过Thread包装来直接运行, // Thread thread = new Thread(fk); // thread.start(); // 也可以提交给ExecuteService来运行, // ExecutorService exec = Executors.newCachedThreadPool(); // Future<String> future = exec.submit(myCallable1); // 并且还可以经过v get()返回运行结果, // 在线程体没有运行完毕的时候,主线程一直堵塞等待,运行完则直接返回结果。 fk.run(); System.out.println(fk.get()); } } class MyFutureTask extends FutureTask { public MyFutureTask(Callable callable) { super(callable); } // 当线程运行结束,则运行done方法。@Override public void done() { // 此处通常用来计算任务运行耗时解析。html
System.out.println("done"); } }java