Java Concurrency - Thread Group

2024. 10. 19. 17:34JAVA & SPRING/자바

반응형

Thread Group


A thread group represents a set of threads. In addition, a thread group can also include other thread groups. The thread groups form a tree in which every thread group except the initial thread group has a parent.

The relationship between ThreadGroup and Thread is as simple as their literal meanings; each Thread necessarily exists in a ThreadGroup, and a Thread cannot exist independently of a ThreadGroup.

A thread group has a name and maximum priority. The name is specified when creating the group and cannot be changed. The group's maximum priority is the maximum priority for threads created in the group. It is initially inherited from the parent thread group but may be changed using the setMaxPriority method.

 

A possible structure


 

1. The system thread group created by the JVM is a group of threads used to handle the JVM's system tasks, such as object destruction, GC, and so on.

 

2. The immediate subthread group of the system thread group is the main thread group, which contains at least one main thread for executing the main method.

 

3. The subthread groups of the main thread group are the thread groups created by the application.

 

Code

public class ConstructDemo {
    public static void main(String[] args) {
        ThreadGroup subThreadGroup1 = new ThreadGroup("subThreadGroup1");
        ThreadGroup subThreadGroup2 = new ThreadGroup(subThreadGroup1,"subThreadGroup2");
        System.out.println("subThreadGroup1 parent name is : " + subThreadGroup1.getParent().getName());
        System.out.println("subThreadGroup2 parent name is : " + subThreadGroup2.getParent().getName());
    }
}

 

Output

subThreadGroup1 parent name is : main
subThreadGroup2 parent name is : subThreadGroup1

 

Method Introductions


Code

public class ThreadGroupMethodCase {
    public static void main(String[] args) throws InterruptedException {
        ThreadGroup subgroup1 = new ThreadGroup("subgroup1");
        Thread t1 = new Thread(subgroup1, "t1 in subgroup1");
        Thread t2 = new Thread(subgroup1, "t2 in subgroup1");
        Thread t3 = new Thread(subgroup1, "t3 in subgroup1");
        t1.start();
        Thread.sleep(50);
        t2.start();
        int activeCount = subgroup1.activeCount();
        System.out.println("Active thread in " + subgroup1.getName() + "thread group: " + activeCount);

        ThreadGroup subgroup2 = new ThreadGroup("subgroup2");
        Thread t4 = new Thread(subgroup2, "t4 in subgroup2");

        ThreadGroup currentThreadGroup = Thread.currentThread().getThreadGroup();
        int activeGroupCount = currentThreadGroup.activeGroupCount();
        System.out.println("Active thread group in " + currentThreadGroup.getName() + "thread group: " + activeGroupCount);

        System.out.println("Prints information about currentThreadGroup to the standard output:");
        currentThreadGroup.list();
    }
}

 

activeCount(): Returns an estimate of the number of live platform threads in this thread group and its subgroups.

activeGroupCount(): Returns an estimate of the number of groups in this thread group and its subgroups.

getMaxPriority(): Returns the maximum priority of this thread group.

getName(): Returns the name of this thread group.

getParents(): Returns the parent of this thread group.

interrupt(): Interrupts all live platform threads in this thread group and its subgroups.

list(): Prints information about this thread group to the standard output,

else

 

Output

Active thread in subgroup1thread group: 1
Active thread group in mainthread group: 2
Prints information about currentThreadGroup to the standard output:
java.lang.ThreadGroup[name=main,maxpri=10]
    Thread[#1,main,5,main]
    Thread[#28,Monitor Ctrl-Break,5,main]
    java.lang.ThreadGroup[name=subgroup1,maxpri=10]
    java.lang.ThreadGroup[name=subgroup2,maxpri=10]

 


https://docs.oracle.com/en/java/javase/23/docs/api/java.base/java/lang/ThreadGroup.html#

https://medium.com/javarevisited/java-concurrecy-5-what-is-a-thread-group-what-is-its-function-2450d95222be

반응형