当ExecutorService在Java中关闭时显示友好消息?

时间:2016-04-20 20:41:32

标签: java multithreading threadpool executorservice

所以,我发现了一个用Java关闭执行程序服务的代码 -

dp.ToolTip = "#SERIESNAME : X=#VALX, Y=#VALY";

在上面的代码中,您看到客户端应用程序(控制台或Web)变为空白,直到线程计数* 30秒用完为止。如何在此处显示友好留言?

2 个答案:

答案 0 :(得分:0)

例如,我已经阻止了执行程序10秒钟进行测试。

ExecutorService executor = Executors.newFixedThreadPool(4);
executor.submit(() -> {
    try {
        Thread.sleep(10_000);
    } catch (Exception e) {
        e.printStackTrace();
    }
});
executor.shutdown();
System.out.println("The executor is shutting down, "+
                   "why not make yourself a cup of tea whilst you wait?");
try {
    executor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
    logger.error(e);
}

答案 1 :(得分:0)

如果您想在等待时显示定期消息,可以这样做(例如每1秒显示一条消息,最多30秒):

Project A
 |
 src
   |
   SomePackage
      |
      A.java
Project B
 |
 src
   |
   AnotherPackage
   |      |
   |      B.java
   |
   SomeOtherPackageInSrc
       |
       C.java