Thread group usage

Thread group

You can assign a thread to a thread group. There can be thread objects or thread objects in the thread group. In this way, the thread group can be similar to a tree.

The use of thread groups is mainly to facilitate the management and maintenance of threads:

Example

(organize multiple threads through thread groups and stop all threads in batches):

1. Create threads A and B

public class ThreadA extends Thread {
    // Set new constructor, pass in thread group object
    public ThreadA(ThreadGroup group, String name) {
        super(group, name);
    }
 
    public void run() {
        while (true) {
            System.out.println("threadA...");
            // Stop according to stop tag
            if (this.isInterrupted()) {
                System.out.println("Current thread:" + this.getName() + "Stop it!");
                break;
            }
        }
    }
}
 
public class ThreadB extends Thread {
 
    public ThreadB(ThreadGroup group, String name) {
        super(group, name);
        // TODO Auto-generated constructor stub
    }
 
    public void run() {
        while (true) {
            System.out.println("threadB...");
            // Stop according to stop tag
            if (this.isInterrupted()) {
                System.out.println("Current thread:" + this.getName() + "Stop it!");
                break;
            }
        }
    }
}

2. Specific test Client

public class Client {
 
    public static void main(String[] args) {
        try {
            // Create thread group
            ThreadGroup group = new ThreadGroup("Custom thread group");
            ThreadA threadA = new ThreadA(group, "threadA");
            ThreadB threadB = new ThreadB(group, "threadB");
            // open
            threadA.start();
            threadB.start();
            Thread.sleep(1000);
            // Output thread group information
            System.out.println("current thread size :" + group.activeCount());
            System.out.println("current thread group name :" + group.getName());
 
            // Stop all threads in thread group
            System.out.println("current thread group stop start...");
            group.interrupt();
            System.out.println("current thread group stop end!");
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


The above code organizes multiple threads through the thread group, and can use the thread group's interrupt() method to stop multiple contained threads.
Output after operation:

...

threadB...
threadB...
current thread size :2
current thread group name: Custom thread group
current thread group stop start...
threadB...
threadA...
current thread group stop end!
Current thread: threadA stopped!
Current thread: threadB stopped!

Remarks:

When each thread is instantiated, if the thread group to which it belongs is not specified, these threads will automatically belong to the thread group to which the current thread object belongs, that is to say, subthreads are implicitly added to a thread group. For example: create multiple threads in the main method. If you don't explicitly set the thread group to which you belong, these threads by default belong to the thread group of main.
The thread group where the main method is located is main, and the parent thread group of the main thread is the jvm thread group system, which can be obtained in main through the following methods:


 

Thread.currentThread().getThreadGroup().getName();
Thread.currentThread().getThreadGroup().getParent().getName()

 

Tags: jvm

Posted on Sat, 09 Nov 2019 11:06:35 -0800 by smarty_pockets