日期:2014-05-20 浏览次数:20816 次
public class gggg {
public class MRCC implements Callable<Integer>{
public Integer call() throws Exception {
int i = 0;
i = getI();
return i;
}
}
public static int getI(){
while(true){
//模拟一个调用可能需要很长时间才会返回,或者不返回结果的接口(只是模拟,并且这个调用接口不可以更改)
}
}
public static void main(String[] args) {
gggg g = new gggg();
ExecutorService e = Executors.newSingleThreadExecutor();
Future<Integer> f = e.submit(g.new MRCC());
try {
f.get(4000, TimeUnit.MILLISECONDS);
System.out.println("获得了返回值");
} catch (InterruptedException e1) {
System.out.println("中断错误");
} catch (ExecutionException e1) {
System.out.println("执行错误");
} catch (TimeoutException e1) {
f.cancel(true);
System.out.println("超时");
}
System.out.println(f.isCancelled() + "," + f.isDone());
System.out.println("关闭");
e.shutdown();
}
}