Вопрос

I was wondering, what are the advantages of assigning threads to a thread group instead of containing them all in one (The Main) group?

Assuming there are 10 or more constantly active threads, and a couple of threads been initiated every now and again as the application requires, how would one approach grouping these?

Thanks, Adam.

Это было полезно?

Решение

There is no advantage at all. ThreadGroups are there for backward compatibility, but I've never seen them used.

Here's what Brian Goetz (author of Java Concurrency in Practice - the bible) said about them a long time ago:

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.

Threads now have an uncauht exception handler, and this single reason to use thread groups isn't valid anymore.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top