gpt4 book ai didi

java - 何时应该创建新的线程组

转载 作者:行者123 更新时间:2023-12-02 00:19:28 26 4
gpt4 key购买 nike

我想知道,将线程分配给线程组而不是将它们全部包含在一个(主)组中有何优点?

假设有 10 个或更多持续 Activity 的线程,并且根据应用程序的需要不时启动几个线程,如何将这些线程分组?

谢谢,亚当。

最佳答案

没有任何优势。 ThreadGroup 的存在是为了向后兼容,但我从未见过它们被使用。

这是 Brian Goetz(Java Concurrency in Practice 的作者 - 圣经)said about them很久以前:

The ThreadGroup class was originally intended to be useful in structuring collectionsof threads into groups. However, it turns out that ThreadGroup is not all that useful. You are better off simply using the equivalent methods in Thread. ThreadGroup does offer one useful feature not (yet) present in Thread: the uncaughtException() method. When a thread within a thread group exits becauseit threw an uncaught exception, the ThreadGroup.uncaughtException() method is called. This gives you an opportunity to shut down the system, write a message to a log file, or restart a failed service.

线程现在有一个意外的异常处理程序,并且使用线程组的这个单一原因不再有效。

关于java - 何时应该创建新的线程组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11302569/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com