|Threads of Control|
Every Java thread is a member of a thread group. Thread groups provide a mechanism for collecting multiple threads into a single object and manipulating those threads all at once, rather than individually. 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
ThreadGroupclass in the
The runtime system puts a thread into a thread group during thread construction. When you create a thread, you can either allow the runtime system to put the new thread in some reasonable default group or you can explicitly set the new thread's group. The thread is a permanent member of whatever thread group it joins upon its creation--you cannot move a thread to a new group after the thread has been created.
The Default Thread GroupIf you create a new Thread without specifying its group in the constructor, the runtime system automatically places the new thread in the same group as the thread that created it (known as the current thread group and the current thread, respectively). So, if you leave the thread group unspecified when you create your thread, what group contains your thread?
When a Java application first starts up, the Java runtime system creates a
main. Unless specified otherwise, all new threads that you create become members of the
Note: If you create a thread within an applet, the new thread's group may be something other than
main, depending on the browser or viewer that the applet is running in. Refer to Threads in Appletsfor information about thread groups in applets.
Many Java programmers ignore thread groups altogether and allow the runtime system to handle all of the details regarding thread groups. However, if your program creates a lot of threads that should be manipulated as a group, or if you are implementing a custom security manager, you will likely want more control over thread groups. Continue reading for more details!
Creating a Thread Explicitly in a GroupAs mentioned previously, a thread is a permanent member of whatever thread group it joins when its created--you cannot move a thread to a new group after the thread has been created. Thus, if you wish to put your new thread in a thread group other than the default, you must specify the thread group explicitly when you create the thread. The
Threadclass has three constructors that let you set a new thread's group:Each of these constructors creates a new thread, initializes it based on thepublic Thread(ThreadGroup group, Runnable target) public Thread(ThreadGroup group, String name) public Thread(ThreadGroup group, Runnable target, String name)
Stringparameters, and makes the new thread a member of the specified group. For example, the following code sample creates a thread group (
myThreadGroup) and then creates a thread (
myThread) in that group.TheThreadGroup myThreadGroup = new ThreadGroup("My Group of Threads"); Thread myThread = new Thread(myThreadGroup, "a thread for my group");
ThreadGrouppassed into a
Threadconstructor does not necessarily have to be a group that you create--it can be a group created by the Java runtime system, or a group created by the application in which your applet is running.
Getting a Thread's GroupTo find out what group a thread is in, you can call its
getThreadGroupmethod:theGroup = myThread.getThreadGroup();
ThreadGroupClassOnce you've obtained a thread's
ThreadGroup, you can query the group for information, such as what other threads are in the group. You can also modify the threads in that group, such as suspending, resuming, or stopping them, with a single method invocation.
|Threads of Control|