为什么我写的这段代码会没有结果返回?只输出了一个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();
}
}
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();
}
}
解决方案 »
- 求救:急!!!
- 下面这个关于serialVersionUID的解释有些看不懂,请举个例子吧..
- 【急】java里 怎么得到类的已有实例
- 菜鸟求助,编译时找不到符号
- 100分求封装过的POI接口(用来读取word文档),正文有连接。
- eclipse里为什么不能加图片文件
- Java document的谬误?
- 怎么把邮件附件下载到本地。
- 电子新出的《Java 2 编程指南》(JDK 1.3 Version)的那本书好贵哟,到地怎么样呢?
- 静态内部类是不是无法用:外部类名.内部类名 变量名=new 外部类名().new 内部类名();的方式实例化吧?——————————————
- enum关键字问题
- 求助(接口怎么可以用来定义方法)
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();
}