Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are Thread Groups in Java?

I am wondering why there is so little documentation about Thread Groups on the internet ? Are they still used or they are some stale concepts ? Can some one explain:

  • What they are.

  • What they are used for.

  • If they are stilled used, where ?

  • Give some real application examples (web servers like, maybe).

like image 671
Adelin Avatar asked Mar 22 '16 14:03

Adelin


1 Answers

They are used as a Group of Threads. In a simple application you only need one, but in a more complex application server it makes sense to have one for each application.

why there is so little documentation about Thread Groups on the internet ?

I guess some assume it's a pretty simple idea. Not sure what is missing about it.

Are they still used or they are some stale concept ?

I would image most developers never think about Thread Groups. But I think they are useful in certain situations. We have a library where we have a custom ThreadGroup for resetting thread affinity.

Can some one explain that are they what they are used for, if still used, and give an example.

Mostly in applications servers, each server has it's own collection of threads and can be managed collectively. If you want to monitor or shutdown an application your need to know which threads the application started.

If you start off a thread in a ThreadGroup, every Thread it creates will also be in that thread group. Without this feature, you would have a hard time assigning threads to applications.

From @biziclop: How do you reliably enumerate threads in a group?

You can get the size of activeThreads and enumerate as the ThreadGroup locks on this (for better or worse)

synchronized(threadGroup) {
    Thread[] threads = threadGroup.activeCount();
    threadGroup.enumerate(threads);
    // use threads before the lock is released or it could be wrong.
}
like image 100
Peter Lawrey Avatar answered Oct 11 '22 01:10

Peter Lawrey