Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
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 theThreadGroup
class in thejava.lang
package.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.
If 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
ThreadGroup
namedmain
. Unless specified otherwise, all new threads that you create become members of themain
thread group.
Note: If you create a thread within an applet, the new thread's group may be something other thanmain
, 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!
As 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. TheThread
class 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 runnable) public Thread(ThreadGroup group, String name) public Thread(ThreadGroup group, Runnable runnable, String name)Runnable
andString
parameters, 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");ThreadGroup
passed into aThread
constructor 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.
To find out what group a thread is in, you can call itsgetThreadGroup
method:theGroup = myThread.getThreadGroup();
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.
You can also modify the threads in that group, such as suspending,
resuming, or stopping them, with a single method invocation.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.