Mythread m =new Mythread();
FutureTask futureTask=new FutureTask(m);
new Thread(futureTask,"A").start();
//new Thread(futureTask,"B").start(); --结果会被缓存 效率高
Object o=futureTask.get(); //这个get方法可能会导致阻塞 !把他放在最后
System.out.println(o);
}
}
class Mythread implements Callable<String> {
@Override
public String call() throws Exception {
System.out.println("call");
return "1024";
}
}
细节
1.有缓存
2.结果可能需要等待,会阻塞!