Question

I´m looking for best practices and I´m applying PMD to my Java EE Project, but one rule says that I have to avoid using java.lang.ThreadGroup, and I´m using it right now.

The rule says that is not safe, and I want to know: Why? Thanks

Was it helpful?

Solution

The concept of using ThreadGroup for security reasons has been abandoned as there is no controllable relationship between a Thread(Group) and the actual code it is executing. Even the most privileged thread could run insecure code and thus elevate that code to an undesired level. Therefore the code being executed itself (and its origin) is used for deciding which permission it has. So the executing thread and its group does not play any role to security in any way.

So after that, ThreadGroup offers no real functionality. It just became obsolete, only maintained for historical reasons. The only functionality which did not work without was uncaughtException(Thread t, Throwable e). But since Java 5 there is Thread.setUncaughtExceptionHandler( UncaughtExceptionHandler) so now even that works without thread groups.

Yes, a lot of ThreadGroups methods are not thread-safe and there was no attempt to fix them, just because they’re obsolete anyway.

Joshua Bloch writes in “Effective Java”:

Thread groups are best viewed as an unsuccessful experiment, and you should simply ignore their existence.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top