Share on Google+Share on Google+

Java ThreadGroup

Advertisement
In this segment of tutorial we will learn about the Java Thread Group. Then We will create an example and use the Thread with the Thread Group.

  • Java Thread Group creates the group of the threads.
  • Each thread group has its name.
  • Each thread uses the thread group in its object creation


Java Thread Group Example
public class Threadgroup {

	public static void main(String[] args) {

		ThreadGroup grp = new ThreadGroup("group1");
		mythread m1 = new mythread(grp, "thread1");
		mythread m2 = new mythread(grp, "thread2");
		mythread m3 = new mythread(grp, "thread3");
		m1.start();
		m2.start();
		m3.start();
	}
}

class mythread extends Thread {
	public mythread(ThreadGroup g, String s) {
		super(g, s);
	}

	@Override
	public void run() {
		System.out.println("Thread group, prority, thread name "
				+ Thread.currentThread());
	}
}

Output :

Thread group, prority, thread name Thread[thread2,5,group1] Thread group, prority, thread name Thread[thread3,5,group1] Thread group, prority, thread name Thread[thread1,5,group1]

Advertisements

Advertisement

Posted on: November 7, 2009 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.