java callable 等待_Java 如何让主线程不阻塞的等待结果,而是去执行其他任务,等到子线程执行完以后通知主线程?...
javapackage test;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class Main {
public static class MyCallable implements Callable {
public Integer call() throws Exception {
Thread.sleep(10000);
return 1;
}
}
public static void main(String[] args) {
MyCallable callable = new MyCallable();
FutureTask task = new FutureTask(callable);
Thread t = new Thread(task);
try {
t.start();
System.out.println(task.get());
System.out.println("here");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}