执行程序服务 – 线程超时

在我探索ExecutorService ,我遇到了一个接受timeout的方法Future.get()

这种方法的Java文档说


如果需要,最多等待计算完成的给定时间,然后检索其结果(如果可用)。

参数:

超时等待的最长时间

单位超时参数的时间单位


根据我的理解,我们在callable上强加了一个超时,我们提交给ExecutorService这样,我的callable将在指定的时间(超时)过后中断

但是根据下面的代码, longMethod()似乎超出了超时(2秒),我很难理解这一点。 谁能请我指出正确的道路?

 import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class Timeout implements Callable { public void longMethod() { for(int i=0; i< Integer.MAX_VALUE; i++) { System.out.println("a"); } } @Override public String call() throws Exception { longMethod(); return "done"; } /** * @param args */ public static void main(String[] args) { ExecutorService service = Executors.newSingleThreadExecutor(); try { service.submit(new Timeout()).get(2, TimeUnit.SECONDS); } catch (Exception e) { e.printStackTrace(); } } } 

我的callable将在指定的时间(超时)过后中断

不对。 任务将继续执行,而超时后您将有一个空字符串。

如果你想取消它:

  timeout.cancel(true) //Timeout timeout = new Timeout(); 

PS正如你现在所拥有的,这个中断将不会产生任何影响。 你没有以任何方式检查它。

例如,此代码考虑了中断:

  private static final class MyCallable implements Callable{ @Override public String call() throws Exception { StringBuilder builder = new StringBuilder(); try{ for(int i=0;i 

然后:

  ExecutorService service = Executors.newFixedThreadPool(1); MyCallable myCallable = new MyCallable(); Future futureResult = service.submit(myCallable); String result = null; try{ result = futureResult.get(1000, TimeUnit.MILLISECONDS); }catch(TimeoutException e){ System.out.println("No response after one second"); futureResult.cancel(true); } service.shutdown(); 

get()的超时是“客户端”等待Future完成的时间。 它对未来的执行没有影响。

 Object result; int seconds = 0; while ((result = fut.get.(1, TimeUnit.SECOND)) == null) { seconds++; System.out.println("Waited " + seconds + " seconds for future"; } 

我的callable将在指定的时间(超时)过后中断

上面的陈述是错误的,通常Future.get是阻塞的。 指定超时允许您以非阻塞方式使用它。

这对于时间关键型应用程序非常有用,如果你需要2秒钟内的结果,那么接收后就意味着你无法做任何事情。