Friday, February 13, 2009

thread group

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 thread group class in the java.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.

The Default Thread Group:

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 named main. Unless specified otherwise, all new threads that you create become members of the main thread group.

Creating a Thread Explicitly in a Group
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 Thread class has three constructors that let you set a new thread's group:
public Thread(ThreadGroup group, Runnable target)
public Thread(ThreadGroup group, String name)
public Thread(ThreadGroup group, Runnable target, String name)
Each of these constructors creates a new thread, initializes it based on the Runnable and String 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.
ThreadGroup myThreadGroup = new ThreadGroup("My Group of Threads");
Thread myThread = new Thread(myThreadGroup, "a thread for my group");
The ThreadGroup passed into a Thread 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.
Thread group Example:
public class ThreadGroupDemo {

class MyThreadGroup extends ThreadGroup {
public void uncaughtException(Thread t, Throwable ex) {
System.err.println("I caught " + ex);
public MyThreadGroup(String name) {

public static void main(String[] args) {
new ThreadGroupDemo().work();

protected void work() {
ThreadGroup g = new MyThreadGroup("bulk threads");
Runnable r = new Runnable() {
public void run() {
System.out.println(Thread.currentThread().getName() + " started");
for (int i=0; i<5; i++) {
System.out.println(Thread.currentThread().getName() + ": " + i);
try {
} catch (InterruptedException ex) {

// Create and start all the Threads
for (int i = 0; i< 10; i++) {
new Thread(g, r).start();

// List them.
Thread[] list = new Thread[g.activeCount()];
for (int i=0; i) {
if (list[i] == null)
Thread t = list[i];
System.out.println(i + ": " + t);


Anonymous said...

Your Example programms are awesome.Those are very easy to understand. i have a doubt. am trying to get outpur like this. but i am not getting output: Thread A 1
Thread B 2
Threa A 3
Thread B 4 like this..
I searched in google and i got d programm but. its very difficult to understand for begginers. here these are very easy to understand and very simple also. can u send that programm

Post a Comment