【问题标题】:Why aren't my threads timing out when they fail?为什么我的线程在失败时没有超时?
【发布时间】:2012-10-06 15:56:06
【问题描述】:

所以我不久前问了一个问题:Over here 问了一个问题“如果我的线程花费太长时间,我怎样才能让它们被杀死”

我已经实现了那里提到的解决方案,但在某些罕见的线程超时的情况下,程序仍然可能失败/锁定(请参阅:保持 main() 方法打开,并防止程序的进一步 cron 运行)。

这是我正在使用的来源:

 //Iterate through the array to submit them into individual running threads.
    ExecutorService threadPool = Executors.newFixedThreadPool(12);
    List<Future<?>> taskList = new ArrayList<Future<?>>();
    for (int i = 0; i < objectArray.length; i++) {
        Future<?> task = threadPool.submit(new ThreadHandler(objectArray[i], i));
        taskList.add(task);
        Thread.sleep(500);
    }

    //Event handler to kill any threads that are running for more than 30 seconds (most threads should only need .25 - 1 second to complete.
    for(Future future : taskList){
        try{
            future.get(30, TimeUnit.SECONDS);
        }catch(CancellationException cx){ System.err.println("Cancellation Exception: "); cx.printStackTrace();
        }catch(ExecutionException ex){ System.err.println("Execution Exception: ");ex.printStackTrace();
        }catch(InterruptedException ix){ System.err.println("Interrupted Exception: ");ix.printStackTrace();
        }catch(TimeoutException ex) {future.cancel(true);}
    }
    threadPool.shutdown();
    threadPool.awaitTermination(60, TimeUnit.SECONDS);

所以我的问题是:实现了这段代码后,为什么执行器服务没有在 30 秒时中断。

【问题讨论】:

  • 注意:每个任务的超时时间都会增加。例如如果第一个任务需要 29 秒,那么您的第二个任务需要 59 秒才能完成。
  • 你能评论一下如何解决这个问题吗?因为那不是故意的。
  • 我已经为导致超时时间较长的原因之一添加了答案/解决方案。

标签: java multithreading timeout executorservice


【解决方案1】:

Java Concurrency in Practice 一书中有一整章专门的任务取消。从我读过的内容来看,任务取消必须在 finally 块中,以确保任务始终结束。

    try{
            future.get(30, TimeUnit.SECONDS);

        } catch (TimeoutException e) {
           // log error message and falls through to finally block
        } catch (ExecutionException e) {
            throw e;
        } finally {
            future.cancel(true); // interrupt task
        }

在处理 InterruptedException 时必须恢复中断状态。

           catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }

在 ThreadHandler 任务中检查 Thread.currentThread().isInterrupted() 标志,如果传播中断状态为 true,则抛出 InterruptedException。

【讨论】:

    【解决方案2】:

    因为我怀疑您的工作线程仍在运行。您正在调用future.cancel(true);,但所做的只是在线程上设置中断标志——它不会主动中断您正在运行的代码。 “中断”代码的另一种方法是将一些volatile boolean shutdown 标志设置为真并在您的代码中进行测试。在此处查看more details about interrupting threads

    您需要确保您的ThreadHandler 代码正确处理中断。例如,它需要在循环或其他代码块中检查Thread.currentThread().isInterrupted()。您还需要确保正确处理InterruptedException 而不仅仅是吞下中断。

    有关线程中断的更多信息,请参阅my answer here

    【讨论】:

    • Hrm,用我目前的数据结构真的很难处理。所有这些都传递给的方法非常简单:本质上它执行 object.run() ,因此每个对象都有自己的线程。根本没有循环运行,如果我执行循环,我最终会多次使用每个对象,这会导致很多问题。
    • object.run() 方法中,您必须能够检测线程是否被中断。这意味着您将不得不在代码中测试Thread.currentThread().isInterrupted()。这些对象在做什么?
    • 嗯,每个线程都有自己的一组工作要做,但他们正在从在线资源中提取特定数据来预测事件......在不破坏 NDA 的情况下我不能说更多。
    • 不用担心。我的观点是没有简单的方法来停止代码没有代码检查中断或某些共享变量。见这里:techrepublic.com/article/interrupting-java-threads/5144546
    • 所以,我的基本问题是我认为 http 请求超时存在问题。问题是我只使用了获取它们的“基本”方法:示例: URL urlObject = new URL("myUrl"); InputStream in = urlObject.openStream(); 从那里我只使用输出流并写入一个文件。假设我连接的网站通过 syn/ack 发送,但由于网络延迟或其他问题甚至没有完成发送文件,程序会挂起。我没有“真正”的方式来放置在一个 thread.isinterrupted 因为它不会去
    【解决方案3】:

    每个任务的超时时间都在增加,这可能不是预期的。相反,您可以在超时后关闭线程池并取消其余部分。

    threadPool.shutdown();
    threadPool.awaitTermination(30, TimeUnit.SECONDS);
    threadPool.shutdownNow(); // interrupt any running tasks.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-06
      • 2019-06-11
      • 1970-01-01
      • 1970-01-01
      • 2016-07-10
      • 1970-01-01
      相关资源
      最近更新 更多