码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-05-15 15:56:55      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:public   java   return   

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());`的情况。主线程依赖这个工作线程的值。那么会不会出现先执行了主线程呢?



答案地址


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

标签:public   java   return   

原文地址:http://wufanxin.blog.51cto.com/7318130/1651500

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!