Home > Java > javaTutorial > ThreadGroup in Java

ThreadGroup in Java

王林
Release: 2024-08-30 16:07:45
Original
418 people have browsed it

ThreadGroup in java can be defined as a collection of threads created to work as a unit, ThreadGroup in java is generally used when there is a need to perform a combined operation on a group of threads; ThreadGroup offers an efficient way to manage multiple threads.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

Here is a syntax of how ThreadGroup is created and used in Java.

package <packagename>;
public class <mainclassname>{
public static void main(String args[]){
// creating threadgroup object
ThreadGroup grp= new ThreadGroup(“<parent thread name>”);
// creating threads belonging to given thread group
<ThreadClassName> one =new <ThreadClassName>(“<threadname1>”, grp);
// creating first member of thread group
<ThreadClassName> two =new <ThreadClassName>(“<threadname2>”, grp);
// creating second member of thread group
}
}
class <ThreadClassName> extends Thread{
<ThreadClassName>(String threadName, ThreadGroup grp){
super(threadName,grp); // call to parent class constructor
start();      // start the thread
}
public void run(){
// implement required logic
}
}
Copy after login

In the above syntax, we have created a ThreadGroup instance and created two threads that are a part of the thread group. We have a main class, and a thread class, is the parent thread name’s name.

How ThreadGroup works in Java?

  • A Thread Group is a collection of multiple related threads. Thread group allows developers to handle multiple java threads simultaneously and is available in java.lang package.
  • Internally thread group can be thought of as a tree in which each thread has a parent, except the parent thread that does not have a parent associated with it.
  • It is to be noted that a thread belonging to a particular thread group has access to information about the same thread group to which the thread belongs; it does not have any information about its parent thread group or any other thread group.

Here is the description of constructors available in java.lang.ThreadGroup:

Constructor Description
public ThreadGroup(String name) This constructor is used for creating a new thread group, the parent of this thread group is the same as the parent group of the currently executing thread. This constructor may throw SecurityException in case the currently running thread does not have permission to create a thread group.
public ThreadGroup(ThreadGroupparentgroup,String name) This constructor creates a thread group with a specified thread group as a parent and specified name. This constructor may throw NullPointerException in case the specified thread group is null, and SecurityException may be thrown in case the currently running thread does not have permission to create a thread group.
Constructor

Description
Method Description
int activeCount() This method returns the number of active running threads available in a given thread group.
int activeGroupCount() This method returns the number of active thread groups running.
destroy() This method destroys the thread group and its sub groups if available.
int enumerate(Thread arr[]) Call to this method puts the thread available in invoking thread group into the group array of threads.
int enumerate(Thread arr[], boolean recurse) Call to this method puts the thread available in invoking thread group into the group array of threads; if the recursive flag is true, then threads in subgroups are also added to the group.
int enumerate(ThreadGroup[] thgrp) This method puts the subgroups of the invoking thread group into the thread group array.
int enumerate(ThreadGroup[] thgrp, boolean recursive) This method puts the subgroups of the invoking thread group into the thread group array; if the recursive flag is set to true, then all subgroups of subgroups are added to the group array.
public ThreadGroup(String name) This constructor is used for creating a new thread group, the parent of this thread group is the same as the parent group of the currently executing thread. This constructor may throw SecurityException in case the currently running thread does not have permission to create a thread group.
public ThreadGroup(ThreadGroupparentgroup,String name) This constructor creates a thread group with a specified thread group as a parent and specified name. This constructor may throw NullPointerException in case the specified thread group is null, and SecurityException may be thrown in case the currently running thread does not have permission to create a thread group.

Here is the list of some important methods available in java.lang.ThreadGroup:

Method

Description

int activeCount() This method returns the number of active running threads available in a given thread group.
int activeGroupCount() This method returns the number of active thread groups running.
destroy() This method destroys the thread group and its sub groups if available.
int enumerate(Thread arr[]) Call to this method puts the thread available in invoking thread group into the group array of threads.
int enumerate(Thread arr[], boolean recurse) Call to this method puts the thread available in invoking thread group into the group array of threads; if the recursive flag is true, then threads in subgroups are also added to the group.
int enumerate(ThreadGroup[] thgrp) This method puts the subgroups of the invoking thread group into the thread group array.
int enumerate(ThreadGroup[] thgrp, boolean recursive) This method puts the subgroups of the invoking thread group into the thread group array; if the recursive flag is set to true, then all subgroups of subgroups are added to the group array.

Apart from the above-listed methods, there are other methods as well which can be used as per need.

Example of ThreadGroup in Java

Given below is the example of ThreadGroup in Java: Example

package com.educba.threadgroupdemo;
import java.lang.*;
class ThreadDemo extends Thread
{
ThreadDemo(String threadname, ThreadGroup thgrp)
{
super(thgrp, threadname);
start();
}
public void run()
{
// implement required logic inside run method
for (int i = 0; i < 2000; i++)
{
try
{
Thread.sleep(20);
}
catch (InterruptedException e)
{
System.out.println("InterruptedException Exception encountered");
}
}
}
}
public class ThreadGroupDemo
{
public static void main(String args[])
{
// creating the thread group
ThreadGroup grp = new ThreadGroup("parent-thread");
// creating new thread and adding to thread group
ThreadDemo t1 = new ThreadDemo("first", grp);
System.out.println("Starting first thread");
// creating another thread and adding to thread group
ThreadDemo t2 = new ThreadDemo("two", grp);
System.out.println("Starting second thread");
// finding the number of active threads
System.out.println("Number of active threads running in thread group: "
+ grp.activeCount());
}
}
Copy after login
Here we will see how to thread group is used in java.

Code:

ThreadGroup in Java

Output:

Conclusion The above article clearly explains the working of thread group in java; it is an inbuilt way to manage multiple threads. The java code examples described above how to thread group is used in java applications.

The above is the detailed content of ThreadGroup in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template