2016-04-20 133 views
1

所以,我V“已经下用Java关停执行服务作为代码 - 上面当在Java中关闭ExecutorService时显示友好的消息?

public void shutdownExecutor() { 
    executor.shutdown(); 
    try { 
     executor.awaitTermination(requestInfo.getUploadThreadCount() * 30, 
       TimeUnit.SECONDS); 
    } catch (InterruptedException e) { 
     logger.error(e); 
    } 
} 

在代码中,你看到的客户端应用程序(控制台或网络)变成空白,直到线程数* 30秒用完。我如何在这里显示友好的信息?

+2

在调用awaitTermination之前添加System.out.println(“Friendly message”);? (目前还不清楚你需要一条消息是否“友好”的意义) –

回答

0

如果你的意思是要显示在等待周期报文,你可以像这样(例如显示消息每隔1秒,最多30秒):

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); 
} 
相关问题