使用ExecutorService
如问题中所述,无限循环不被视为最佳解决方案。 Instead, the ExecutorService provides a built-in mechanism to manage task completion: the shutdown() and awaitTermination() methods.
Using shutdown() and awaitTermination()The ExecutorService interface offers the shutdown() method, which signals the ExecutorService to no longer accept new tasks.一旦当前提交的任务完成了,执行人员服务将终止。
等待任务完成,则采用了eakeattermination()方法。此方法采用两个参数:超时:指定任务完成的最大时间。 long.max_value超时可确保ategentermination()方法阻止所有任务完成。如果任务完成时间敏感,则可以指定有限的超时。结论[&& && &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& && && && &&&华()方法和等待方法提供了一个可靠且有效的方式,以等待所有线程,以等待所有求助于求助于Infinite loops nofnite loops。这种方法简化了任务管理,并以受控方式确保线程完成。
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3