简体   繁体   English

嵌套JavaFX ExecutorService关闭

[英]Nested JavaFX ExecutorService shutdown

For example, if you have an ExecutorServic(ES) in JavaFX that starts a task, which in turn contains another ES and starts new tasks. 例如,如果您在JavaFX中有一个ExecutorServic(ES)来启动一个任务,该任务又包含另一个ES并启动新任务。 - What will happen to 2nd ES's threads when the 1st ES is shut down. -当第一个ES关闭时,第二个ES的线程将发生什么。 I assume they would not be shut down (as they are not if only main thread is shut down, if I understand correctly). 我假设它们不会被关闭(因为如果我理解正确的话,只有主线程被关闭时它们不会关闭)。

When you establish an executor service backed by a thread pool, it is vital that you shutdown that service and its threadpool. 建立由线程池支持的执行程序服务时,至关重要的是关闭该服务及其线程池。 If your app exits, your task and the pool may continue onward, as a sort of zombie. 如果您的应用程序退出,则您的任务和池可能会像僵尸一样继续前进。 Be sure the hooks into the lifecycle of your app (launching, quitting, and such) is being properly used to start and stop your executors and their thread pools. 确保正确使用了应用程序生命周期中的钩子(启动,退出等)来启动和停止执行程序及其线程池。

Also, you can reuse an executor service and its thread pool rather than spawning a new one. 另外,您可以重用执行程序服务及其线程池,而不必产生新的服务。 There are times where it makes sense to have a separate pool, but you should be very clear about your motivation in doing so. 有时候,有一个单独的池是有意义的,但是您应该非常清楚这样做的动机。

When you do establish two or more executor service, they and their backing thread pools are separate. 当您建立两个或多个执行程序服务时,它们及其后备线程池是分开的。 Shutting down one has no effect on the other. 关闭一个对其他没有任何影响。

By the way, it can be sensible in some cases to have a task in a scheduled executor service schedule yet another task. 顺便说一句,在某些情况下在计划的执行者服务计划中包含一个任务又是另一个任务是明智的。 But it could also be a “code smell”, a clue about a poor design. 但这也可能是“代码异味”,这是有关不良设计的线索。 You've not given enough detail to tell which is the case here, but I suggest you think about it and search the many other existing posts on executors. 您没有提供足够的细节来说明这里的情况,但我建议您考虑一下并搜索执行器上许多其他现有的帖子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM