ThreadGroup in JavaJava provides a convenient way to group multiple threads in a single object. In such a way, we can suspend, resume or interrupt a group of threads by a single method call. Note: Now suspend(), resume() and stop() methods are deprecated.Java thread group is implemented by java.lang.ThreadGroup class. A ThreadGroup represents a set of threads. A thread group can also include the other thread group. The thread group creates a tree in which every thread group except the initial thread group has a parent. A thread is allowed to access information about its own thread group, but it cannot access the information about its thread group's parent thread group or any other thread groups. Constructors of ThreadGroup classThere are only two constructors of ThreadGroup class.
Methods of ThreadGroup classThere are many methods in ThreadGroup class. A list of ThreadGroup methods is given below.
Let's see a code to group multiple threads. Now all 3 threads belong to one group. Here, tg1 is the thread group name, MyRunnable is the class that implements Runnable interface and "one", "two" and "three" are the thread names. Now we can interrupt all threads by a single line of code only. ThreadGroup ExampleFile: ThreadGroupDemo.java Output: one two three Thread Group Name: Parent ThreadGroup java.lang.ThreadGroup[name=Parent ThreadGroup,maxpri=10] Thread Pool Methods Example: int activeCount()Let's see how one can use the method activeCount(). FileName: ActiveCountExample.java Output: Starting the first Starting the second The total number of active threads are: 2 Thread Pool Methods Example: int activeGroupCount()Now, we will learn how one can use the activeGroupCount() method in the code. FileName: ActiveGroupCountExample.java Output: Starting the first Starting the second The total number of active thread groups are: 1 the second thread has finished executing the first thread has finished executing Thread Pool Methods Example: void destroy()Now, we will learn how one can use the destroy() method in the code. FileName: DestroyExample.java Output: Starting the first Starting the second the first thread has finished executing the second thread has finished executing the child group is destroyed. the parent group is destroyed. Thread Pool Methods Example: int enumerate()Now, we will learn how one can use the enumerate() method in the code. FileName: EnumerateExample.java Output: Starting the first Starting the second Thread the first is found. Thread the second is found. the first thread has finished executing the second thread has finished executing Thread Pool Methods Example: int getMaxPriority()The following code shows the working of the getMaxPriority() method. FileName: GetMaxPriorityExample.java Output: Starting the first Starting the second The maximum priority of the parent ThreadGroup: 10 the first thread has finished executing the second thread has finished executing Thread Pool Methods Example: ThreadGroup getParent()Now, we will learn how one can use the getParent() method in the code. FileName: GetParentExample.java Output: Starting the first Starting the second The ParentThreadGroup for the parent group is main The ParentThreadGroup for the child group is the parent group the first thread has finished executing the second thread has finished executing Thread Pool Methods Example: void interrupt()The following program illustrates how one can use the interrupt() method. FileName: InterruptExample.java Output: Starting the first Starting the second The exception has been encountered java.lang.InterruptedException: sleep interrupted The exception has been encountered java.lang.InterruptedException: sleep interrupted the second thread has finished executing the first thread has finished executing Thread Pool Methods Example: boolean isDaemon()The following program illustrates how one can use the isDaemon() method. FileName: IsDaemonExample.java Output: Starting the first Starting the second The group is not a daemon group. the second thread has finished executing the first thread has finished executing Thread Pool Methods Example: boolean isDestroyed()The following program illustrates how one can use the isDestroyed() method. FileName: IsDestroyedExample.java Output: Starting the first Starting the second The group has not been destroyed. the first thread has finished executing the second thread has finished executing Next TopicShutdownHook Thread in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India