Threads of Control |
In Java, all threads must be a member of a thread group. Thread groups provide a mechanism for collecting multiple threads together into a single object and manipulating those threads all at once through the group rather than individually through the threads themselves. For example, you can start or suspend all the threads within a group with a single method call. Java thread groups are implemented by the ThreadGroup class in the java.lang package.Creating a Thread Explicitly within a Group
The only time that you are allowed to set a thread's group is during creation--a thread can't be moved to a new group after it's been created. Thus, you should specify a thread's group explicitly when you create it. The Thread class has three different constructors that let you set the new thread's group:Each of these constructors requires a ThreadGroup as its first parameter, creates a new thread, initializes it based on the other parameters, and makes the new thread a member of the specified group. For example, the following code sample creates a ThreadGroup named
Thread(ThreadGroup, Runnable)
Thread(ThreadGroup, String)
Thread(ThreadGroup, Runnable, String)
myThreadGroup
and then creates a thread calledmyThread
in that group.The ThreadGroup passed into a Thread constructor does not necessarily have to be a group that you created--it could be a group that was created by the Java runtime system, or a group that was created by the application in which your applet is running.ThreadGroup myThreadGroup = new ThreadGroup("My Group of Threads"); Thread myThread = new Thread(myThreadGroup, "a thread for my group");As you know, you don't have to set the thread group when you create a new thread--the Thread class supports several constructors that don't require a ThreadGroup argument. If you don't explicitly set the thread group for a new thread when you create it (which is probably what you've been doing until now), the new thread is automatically placed in the same group as the thread which created it (known as the current thread and the current thread group respectively).
So, if all threads must be in a group, and you haven't created a ThreadGroup, and you create a new thread without specifying its group, what is the thread's group? When an application first starts up, the Java runtime system creates a ThreadGroup named "main". So, unless specified otherwise, all new threads that you create become members of the "main" thread group.
Note: if the thread that you are creating is created by an applet, it's possible that the new thread's group will be something other than "main". It depends on the browser or viewer that the applet is running in.Getting a Thread's Group
To find out what group a thread is in, you can call itsgetThreadGroup()
method.theGroup = myThread.getThreadGroup();The ThreadGroup Class
Once you've obtained a thread's ThreadGroup, you can query the group for information (such as what other threads are in the group), or you can modify the threads in that group (such as suspend, resume or stop them) with a single method call.See also
java.lang.ThreadGroup
Threads of Control |