昆山网站备案,网站头部设计优化,做网站营销公司,客户说做网站没效果怎么回答好在我的代码中#xff0c;我使用CompletionService和ExecutorService来启动一堆Thread来执行某些任务(这可能需要很长时间) . 所以我有一个创建ExecutorService和CompletionService的方法#xff0c;然后开始提交线程然后获取结果 . 我想添加一个关闭钩子#xff0c;以便优雅…在我的代码中我使用CompletionService和ExecutorService来启动一堆Thread来执行某些任务(这可能需要很长时间) . 所以我有一个创建ExecutorService和CompletionService的方法然后开始提交线程然后获取结果 . 我想添加一个关闭钩子以便优雅地关闭执行程序(我知道可能我应该处理释放资源而不是 Actuator 关闭但在我的情况下每个线程都有自己的资源所以优雅地关闭它们可以是一个很好的解决方案我假设) .因此我编写以下代码public Class myClass{...private CompletionService completion;final long SHUTDOWN_TIME TimeUnit.SECONDS.toSeconds(10);...public Message executeCommand(Message request){final ExecutorService executor Executors.newFixedThreadPool(30);completion new ExecutorCompletionService(executor);....//submit and take resultsRuntime.getRuntime().addShutdownHook(new Thread(){Overridepublic void run() {logger.debug(Shutting down executor);try {if (!executor.awaitTermination(SHUTDOWN_TIME, TimeUnit.SECONDS)) {logger.debug(Executor still not terminate after waiting time...);List notExecuted executor.shutdownNow();logger.debug(List of dropped task has size droppedTasks.size());}}catch(InterruptedException e){logger.error(,e);}}});}}您认为这是一个合理的解决方案还是使用本地类注册和取消注册关闭钩子是不安全的提前致谢问候