【面试题】JAVA 一个线程依赖另外一个线程的结果

public class Main {

public static class MyCallable implements Callable<Integer>{
public Integer call() throws Exception {

return 1;
}

}
public static void main(String[] args) {
MyCallable callable=new MyCallable();
FutureTask<Integer> task=new FutureTask<Integer>(callable);
Thread t=new Thread(task);
try {
t.start();
System.out.println(task.get());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
操作系统分配线程执行权是随机的。那么有没有可能出现先执行了`System.out.println(task.get());`的情况。主线程依赖这个工作线程的值。那么会不会出现先执行了主线程呢?

 

 

答案地址

posted @ 2015-05-15 10:00  techfox  阅读(794)  评论(0编辑  收藏  举报