Friday 15 May 2015

Grouping Threads in Java

 Earlier you learned a little about the ThreadGroupclass, which is used to group threads together. Grouping threads is sometimes useful because it allows you to control multiple threads as a single entity. For example, the ThreadGroupclass has suspend and resumemethods, which can be used to suspend and resume the entire group of threads. What you haven't learned is how to actually manage thread groups, which is the focus of this section.
Every thread in the Java runtime system belongs to a thread group. You may be wondering how this is possible, considering the fact that you saw earlier how to create threads with no mention of a thread group. If you create a thread without specifying a thread group, the thread is added to the group that the current thread belongs to. The current thread is the thread where the new thread is created from. In some cases, there may not be a current thread, in which case the Java runtime system adds the thread to a default thread group called main.
You associate threads with a particular group upon thread creation. There is no way to alter the group membership of a thread once it has been created; in other words, you get one opportunity to specify the permanent group for a thread when you create the thread. The Thread class includes constructors for specifying the thread group for the thread:
Thread(ThreadGroup, String)
Thread(ThreadGroup, Runnable)
Thread(ThreadGroup, Runnable, String)
Each constructor takes a ThreadGroupobject as the first parameter. The first constructor also takes a string parameter, allowing you to give the thread a name. The last two constructors take a Runnableobject as the second parameter, which is typically an object of your own concoction that implements the Runnableinterface. Finally, the last constructor also takes a string parameter, allowing you to name the thread.
Before you can create any threads, you need to create a ThreadGroupobject. The ThreadGroup class defines two constructors, which follow:
ThreadGroup(String name)
ThreadGroup(ThreadGroup parent, String name)
The first constructor simply creates an empty thread group with the specified name. The second constructor does the same thing, but places the new thread group within the thread group specified in the parent parameter. This constructor allows you to nest thread groups.
Take a look at a quick example of creating a thread group and adding a few threads to it. The following code shows how to create and manage a thread group and a couple of member threads:
ThreadGroup group = new ThreadGroup("Queen bee");
Thread t1 = new Thread(group, "Worker bee 1");
Thread t2 = new Thread(group, "Worker bee 2");
t1.start();
t2.start();
...
group.suspend();
...
group.resume();
After the thread group is created, each thread is created and passed the ThreadGroup object. This makes them members of the thread group. Each thread is then started by calling the startmethod of each; the ThreadGroupclass doesn't provide a means to start all the thread members at once. Sometime later the thread group suspends both threads with a call to the suspendmethod. It then gets them running again by calling resume.
You can find out what group a thread belongs to by calling the getThreadGroup method. This method returns the ThreadGroupobject that the thread belongs to. You can then find out the name of the thread group by calling the getNamemethod defined in the ThreadGroupclass. The following code shows how to print the name of the thread group for a particular thread.

ThreadGroup group = t.getThreadGroup(); 
System.out.println(group.getName());

No comments:

Post a Comment