Java – Important Methods of ThreadGroup Class | Code Factory


Index Page : Link

Donate : Link

Medium Link : Link

Applications : Link

1. public final String getName()

  • Returns the name of this thread group.

2. public final int getMaxPriority()

  • Returns the maximum priority of this thread group. Threads that are part of this group cannot have a higher priority than the maximum priority.

3. public final void setMaxPriority(int pri)

  • Sets the maximum priority of the group.
  • The default max priority is 10.
  • Threads in the Thread Group that already have higher priority wouldn’t be affected but for newly added threads this max priority is applicable.
package com.example.thread;

public class Test {
	public static void main(String... args) {
		ThreadGroup tg1 = new ThreadGroup("Group1");
		Thread t1 = new Thread(tg1, "Thread1");
		Thread t2 = new Thread(tg1, "Thread2");
		tg1.setMaxPriority(4);
		Thread t3 = new Thread(tg1, "Thread3");
		System.out.println(t1.getPriority()); // 5
		System.out.println(t2.getPriority()); // 5
		System.out.println(t3.getPriority()); // 4
	}
}

4. public final ThreadGroup getParent()

  • Returns the parent of this thread group.

5. public void list()

  • Prints information about this thread group to the standard output / console.

6. public int activeCount()

  • Returns an estimate of the number of active threads in this thread group and its subgroups. Recursively iterates over all subgroups in this thread group.

7. public int activeGroupCount()

  • Returns an estimate of the number of active groups in this thread group and its subgroups. Recursively iterates over all subgroups in this thread group.

8. public int enumerate(Thread[] list)

  • Copies into the specified array every active thread in this thread group and its subgroups.

9. public int enumerate(Thread[] list, boolean recurse)

  • Copies into the specified array every active thread in this thread group. If recurse is true, this method recursively enumerates all subgroups of this thread group.

10. public int enumerate(ThreadGroup[] list)

  • Copies into the specified array references to every active subgroup in this thread group and its subgroups.

11. public int enumerate(ThreadGroup[] list, boolean recurse)

  • Copies into the specified array references to every active subgroup in this thread group. If recurse is true, this method recursively enumerates all subgroups.

12. public final boolean isDaemon()

  • Tests if this thread group is a daemon thread group. A daemon thread group is automatically destroyed when its last thread is stopped or its last thread group is destroyed.

13. public final void setDaemon(boolean daemon)

  • Changes the daemon status of this thread group.

14. public final void interrupt()

  • Interrupts all threads in this thread group.

15. public final void destroy()

  • Destroys this thread group and all of its subgroups. This thread group must be empty, indicating that all threads that had been in this thread group have since stopped.

. . . . .

package com.example.thread;

/**
 * @author code.factory
 *
 */
public class ThreadGroupTest {
	public static void main(String... args) throws InterruptedException {
		ThreadGroup tg1 = new ThreadGroup("Parent Group");
		ThreadGroup tg2 = new ThreadGroup(tg1, "Child Group");
		MyThread t1 = new MyThread(tg1, "Child Thread 1");
		MyThread t2 = new MyThread(tg1, "Child Thread 1");
		t1.start();
		t2.start();
		System.out.println(tg1.activeCount());
		System.out.println(tg1.activeGroupCount());
		tg1.list();
		Thread.sleep(2000);
		System.out.println(tg1.activeCount());
		System.out.println(tg1.activeGroupCount());
		tg1.list();
	}
}

class MyThread extends Thread {
	MyThread(ThreadGroup tg, String name) {
		super(tg, name);
	}
	
	public void run() {
		System.out.println("Child Thread");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

Output :

2
Child Thread
Child Thread
1
java.lang.ThreadGroup[name=Parent Group,maxpri=10]
    Thread[Child Thread 1,5,Parent Group]
    Thread[Child Thread 1,5,Parent Group]
    java.lang.ThreadGroup[name=Child Group,maxpri=10]
0
1
java.lang.ThreadGroup[name=Parent Group,maxpri=10]
    java.lang.ThreadGroup[name=Child Group,maxpri=10]

. . . . .

Write a program to display all active thread names belongs to system group and it’s child group :

package com.example.thread;

/**
 * @author code.factory
 *
 */
public class ThreadGroupTest {
	public static void main(String... args) throws InterruptedException {
		ThreadGroup tg = Thread.currentThread().getThreadGroup().getParent();
		Thread t[] = new Thread[tg.activeCount()];
		tg.enumerate(t);
		for(Thread t1 : t) {
			System.out.println(t1.getName() + " : " + t1.isDaemon());
		}
	}
}

Output :

Reference Handler : true
Finalizer : true
Signal Dispatcher : true
Attach Listener : true
main : false

6 thoughts on “Java – Important Methods of ThreadGroup Class | Code Factory”

Leave a comment