为什么我写的这段代码会没有结果返回?只输出了一个call()
public class Test1{
private ExecutorService exes = Executors.newFixedThreadPool(1);
public Future<Integer> getR(final int x, final int y){
return exes.submit(new Callable<Integer>(){
public Integer call(){
System.out.println("call()");
return x + y;
}
});
}

public static void main(String[] args){
Test1 myT1 = new Test1();
Future<Integer> myF = myT1.getR(2,3);
if(myF.isDone()){
System.out.println("isDone");
try{
System.out.println(myF.get());
}catch(Exception e){
System.out.println(e);
}
}
myT1.exes.shutdown();
}
}

解决方案 »

  1.   


    public static void main(String[] args){
            Test1 myT1 = new Test1();
            Future<Integer> myF = myT1.getR(2,3);
            if(myF.isDone()){
                System.out.println("isDone");
                try{
                    System.out.println(myF.get());//好像是你调用了这个myF.get()方法,myF.isDone()的状态才变成true啊
                }catch(Exception e){
                    System.out.println(e);
                }
            }
            myT1.exes.shutdown();
        }
    这样是可以的,呵呵,太困了,这个也没有用过,LZ再好好琢磨琢磨吧public static void main(String[] args){
            Test1 myT1 = new Test1();
            Future<Integer> myF = myT1.getR(2,3);
            myF.get()//在这里调用
            if(myF.isDone()){//再判断状态
                System.out.println("isDone");
                try{
                    System.out.println(myF.get());
                }catch(Exception e){
                    System.out.println(e);
                }
            }
            myT1.exes.shutdown();
        }
      

  2.   

    应该是你if(myF.isDone())的问题