当前位置: 首页 > 知识库问答 >
问题:

Service.ShutdownNow()未终止线程

顾俊誉
2023-03-14
            ExecutorService service = Executors.newSingleThreadExecutor();
            Future<Object> prediction = null;
            try {
              prediction = service.submit(() -> {
                // Execute the requested Document Prediction Engine against the XML Document.
                return executeEngine(tenantId, appId, engine, document, predictionParams == null ? new HashMap<>() : new HashMap<>(predictionParams)).get();
              });
              predictionResult = prediction != null ? (PredictionResult) prediction.get(Long.parseLong(System.getProperty(IDR_INTERNAL_TIMEOUT, "30000")),
                      TimeUnit.MILLISECONDS) : null;
              } catch (TimeoutException e) {
              if (prediction != null) {
                LOGGER.debug("Task was cancelled with a {} status", (prediction.cancel(true) ? " successful" : " failure"));
              }
              ExpenseMetrics.internalPredictionTimeout.inc();
              String message = "Prediction took more than allowed milliseconds: " + Long.parseLong(System.getProperty(IDR_INTERNAL_TIMEOUT, "30000")) +
                " fileName: "+ documentFile.getFileName();
              
              if (service != null && !service.isShutdown()) {
                service.shutdownNow();
              }
              service = null;
              throw new IDRExmClientException(message, requestId, ErrorCode.INTERNAL_TIMEOUT);
            } 
            if (service != null && !service.isShutdown()) {
              service.shutdownNow();
            }
            service = null;
List<Callable<Void>> taskList = new ArrayList<Callable<Void>>();
taskList.add(callable1);
taskList.add(callable2);    
ExecutorService executor = null;
List<Future<Void>> futures = null;
long s = System.currentTimeMillis();
try {
  executor = Executors.newFixedThreadPool(THREAD_POOL);
  futures = executor.invokeAll(taskList);    
  executor.shutdown();
  if (!executor.awaitTermination(TOLERANCE_MINUTES, TimeUnit.MINUTES)) {                
    LOGGER.warn("Document predict thread took more than {} minutes to shutdown", TOLERANCE_MINUTES);
    executor.shutdownNow();
  }
} catch (InterruptedException iex) {
  LOGGER.error("Document predict thread was interrupted", iex);
} finally {
  cancelFutures("Predict", futures);
  LOGGER.debug("Document predict thread took: {}", (System.currentTimeMillis() - s));
  if (executor != null && !executor.isShutdown()) {        
    executor.shutdownNow();
  }      
}
executor = null;

共有1个答案

连德义
2023-03-14

来自方法shutDownNow()的Oracle文档:

除了尽力尝试停止处理正在执行的任务之外,没有任何保证。例如,typicalimplementations将通过Thread.Interrupt取消,因此任何未能响应中断的任务都可能永远不会终止。

您可以看到这个答案,它可能会帮助您:ExecutorService没有关闭

 类似资料:
  • 我正在编写一个JavaFX应用程序,我的对象扩展任务提供了JavaFXGUI线程之外的并发性。 我的主要课程是这样的: 我的GUI控制器示例如下(略作抽象): 目前,我的任务只是进行睡眠并打印数字1到10: 我遇到的问题是,一旦任务完成,就好像启动任务的线程继续运行一样。因此,当我按下右上角的“X”退出JavaFX应用程序时,JVM继续运行,我的应用程序不会终止。如果你看一下我的主课,我已经把系统

  • 我尝试构建一个GUI应用程序,从相机抓取帧并在Tkinter GUI中显示它们。Tkinter mainloop在主线程中执行,而gui的帧抓取和更新在单独的线程中进行。 下面的代码作为一个视频流被抓取并正确显示在我的gui窗口中。但是,当我通过单击“x”来调用on_close()方法来关闭gui时,gui将关闭,但程序不会完全终止。最后一次CLI输出将为“Mainloop stopped!”,但

  • 主要内容:1 什么是Java终止线程,2 Thread类终止线程的方法,3 Java终止线程的例子1,4 Java终止线程的例子2,5 Java终止线程的例子3,6 isInterrupted和interrupted方法1 什么是Java终止线程 如果任何线程处于睡眠或等待状态(即,调用sleep()或wait()方法),则在线程上调用interrupt()方法,会抛出InterruptedException中断睡眠或等待状态。如果线程未处于睡眠或等待状态,则调用interrupt()方法将执行

  • 它的工作很好,但当我把它封闭在我的如果不喜欢 这给了我一个例外: org.apache.jasper.jasperexception:/jsp/custmngmt/reports/viewCustomerDepositReportCriteriaForOldReceipt.jsp(356,0)未终止 我已经测试了条件,它确实输入了if部分,但在通过form标记时会导致异常。我不明白。请帮帮我。

  • 问题内容: 当我测试创建子线程的方法的执行时,JUnit测试会在子线程之前终止并杀死它。 我如何强制JUnit等待子线程完成其执行? 谢谢 问题答案: 阅读问题和评论后,似乎您需要的是 一种对异步操作进行单元测试的技术 。doSomething()立即返回,但是您希望测试代码等待其完成,然后进行一些验证。 问题在于该测试无法识别该调用所产生的线程,因此显然它无法等待它们。人们可以想到许多复杂的(可

  • 请帮助我处理这个主线程/父线程将触发子线程。如果我们停止父线程/主线程,它还必须停止所有子线程/子线程 我想用中断做这件事,但做不到。请帮我把代码弄出来 以及如何确保所有子线程都已停止?有什么办法也可以做到这一点吗 提前谢谢! 我正在尝试这样做: 公共类ThreadTest1扩展Thread{私有静态最终记录器LOGGER=Logger.get记录器(mylogger); }