public static void main(String args[]) throws InterruptedException, ExecutionException {
Callable<String> c=new Callable<String>() {
@Override
public String call() throws Exception {
// TODO Auto-generated method stub
Thread.sleep(1000);
return "done ...";
}
};
FutureTask<String> ft=new FutureTask<String>(c);
new Thread(ft).start();
System.out.println("wait to be done ...");
System.out.println(ft.get());
}
执行结果
wait to be done ...
done ...

本文展示了一个使用Java并发编程中FutureTask和Callable接口的示例。通过创建一个Callable任务并将其包装到FutureTask中,然后在新线程上启动,最后获取任务的返回结果。
171万+





