Share on Google+Share on Google+

Java Thread : ThreadGroup

Ads
In this tutorial, you will learn how to use ThreadGroup with example in Java.

Java Thread : ThreadGroup

In this tutorial, you will learn how to use ThreadGroup with example in Java.

getThreadGroup :

Every Java thread belongs to a thread group. Thread group treats all multiple threads into a single object and perform operations on all threads at once.The default thread group is main created by the JVM when first java application is started.

public final ThreadGroup getThreadGroup() : This method returns the thread group of threads to which they belongs. If thread die then it will return null.

Example : In this example we are using ThreadGroup.

public class ThreadGroupExample implements Runnable {

    Thread thread;
    ThreadGroup threadGroup;

    ThreadGroupExample() {
    	threadGroup = new ThreadGroup("Thread Group Example");
        thread = new Thread(threadGroup, this);
        thread.start();
    }

    public void run() {
        System.out.println(thread.getThreadGroup());
    }

    public static void main(String[] args) {
        new ThreadGroupExample();
        
    }
}

Output :

java.lang.ThreadGroup[name=Thread Group Example,maxpri=10]

Advertisements

Ads

Posted on: October 23, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.