当前位置:网站首页>Thread group ThreadGroup uses introduction + custom thread factory class to implement threadfactory interface

Thread group ThreadGroup uses introduction + custom thread factory class to implement threadfactory interface

2022-04-23 14:14:00 pureluckyfish

One 、ThreadGroup Class introduction

        A thread group is a A tree structure ,ThreadGroup Class has some methods to delete or add and maintain this tree , There are also some methods to query the node status and hierarchical relationship of the tree ; Thread group's Access control and linux The genera are similar to the main genus group : A thread is only allowed to access information about its own thread group , However, it is not allowed to access the information of the parent thread group of its thread group or any other thread group .

      

Code example :

package com.yu;

public class ThreadGroupTest {
	public static void main(String[] args) throws Exception {
		ThreadGroup tg_parent = new ThreadGroup(" The parent thread group ");
		ThreadGroup tg_son = new ThreadGroup(tg_parent, " The child thread group ");
		Thread t1 = new Thread(tg_son, () -> {
			System.out.println(" Thread name :" + Thread.currentThread().getName());
			System.out.println(" Thread group name :" + Thread.currentThread().getThreadGroup().getName());
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}, "t1 Threads ");

		Thread t2 = new Thread(tg_parent, () -> {
			System.out.println(" Thread name :" + Thread.currentThread().getName());
			System.out.println(" Thread group name :" + Thread.currentThread().getThreadGroup().getName());
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}, "t2 Threads ");

		Thread t3 = new Thread(tg_parent, () -> {
			System.out.println(" Thread name :" + Thread.currentThread().getName());
			System.out.println(" Thread group name :" + Thread.currentThread().getThreadGroup().getName());
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}, "t2 Threads ");

		System.out.println(" Thread group name :"+t1.getThreadGroup().getName());
		System.out.println(" Whether the thread is in a given thread group :"+t1.getThreadGroup().parentOf(tg_parent));
		System.out.println(" Whether the thread is in a given thread group :"+t1.getThreadGroup().parentOf(tg_son));
		System.out.println(" Number of live threads in thread group :"+tg_parent.activeCount());// Is an evaluation value 
		System.out.println(" Number of thread groups alive in thread group :"+tg_parent.activeGroupCount());// Is an evaluation value 
		System.out.println(" Maximum priority of thread group :"+t1.getThreadGroup().getMaxPriority());

	}
}

Use scenarios : 1 Big task , It can be divided into multiple independent sub threads for concurrent processing , As long as one of the sub threads completes the task condition , Even if it's done , Call threadGroup.interrupt() Method   Other sub threads can be stopped ;2 Big task , It can be divided into multiple independent sub threads for concurrent processing , Finally, wait for the execution of all sub threads to end, and then continue to execute .

Two 、ThreadFactory The interface is introduced

        ThreadFactory It's an interface , only one Thread newThread(Runnable r)  Method ;

        The custom thread factory implementation class produces threads like a factory pipeline according to certain rules , These threads “ They all look alike   ” ;

          Custom thread factory implementation class , You can track when and how many threads are created by the thread pool , You can also customize the thread name , Group , priority , Even directly set all threads as daemon threads . You can customize the thread pool to set the status of all threads in the pool more freely .

Code example :

package com.yu;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class ThreadFactoryTest {

	public static void main(String[] args) throws Exception {
		//  Custom thread factory 
		ThreadFactory threadFactoryOwner = new ThreadFactory() {

			@Override
			public Thread newThread(Runnable r) {
				Thread t = new Thread(r);
				t.setName("threadFactoryOwner-pool-" + t.getId());
				t.setDaemon(true);//  Set to daemons 
				return t;
			}
		};

		//  Create a thread pool using a custom factory 
		ExecutorService executorService = Executors.newFixedThreadPool(10, threadFactoryOwner);
		for (int i = 0; i < 10; i++) {
			executorService.submit(() -> {
				System.out.println(" Thread name :" + Thread.currentThread().getName());
				System.out.println(" Thread group :" + Thread.currentThread().getThreadGroup());
				System.out.println(" Threads ID:" + Thread.currentThread().getId());
				System.out.println("==========================================");
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			});
			Thread.sleep(2000);
		}

	}
}

JDK The default thread factory implementation : When creating a thread pool, specify the thread factory implementation class , If not specified, use JDK Default thread factory .

JDK The default thread factory implementation

3、 ... and 、 The connection between the two

        When customizing the thread factory implementation class, you can use the specified thread group .

版权声明
本文为[pureluckyfish]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231406486413.html