您的位置:首页 > 职场人生

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

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

答案地址

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐