Java - 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 ThreadGroup 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.

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.

Creating a Thread Explicitly in a Group

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. 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.

Getting a Thread's Group
To find out what group a thread is in, you can call its getThreadGroup method:
theGroup = myThread.getThreadGroup();

People who read this post also read :


Look at the way my colleague Wesley Virgin's report begins in this SHOCKING AND CONTROVERSIAL VIDEO.

As a matter of fact, Wesley was in the military-and soon after leaving-he revealed hidden, "mind control" secrets that the government and others used to obtain whatever they want.

THESE are the same methods many celebrities (especially those who "became famous out of nothing") and the greatest business people used to become wealthy and successful.

You probably know how you utilize only 10% of your brain.

That's mostly because most of your BRAINPOWER is UNCONSCIOUS.

Maybe that conversation has even taken place INSIDE OF YOUR own head... as it did in my good friend Wesley Virgin's head about seven years back, while riding an unlicensed, beat-up bucket of a car with a suspended driver's license and with $3.20 on his banking card.

"I'm so frustrated with going through life payroll to payroll! When will I get my big break?"

You've taken part in those types of thoughts, ain't it so?

Your success story is going to start. You just need to take a leap of faith in YOURSELF.


Post a Comment


Twitter Delicious Facebook Digg Stumbleupon Favorites More