线程组ThreadGroup使用介绍+自定义线程工厂类实现ThreadFactory接口

一、ThreadGroup类介绍

        线程组是一个树形结构 ,ThreadGroup类有一些方法可以删除或者新增维护这棵树,也有一些查询树节点状态和层级关系的方法;线程组的权限控制和linux的属主属组类似:线程只允许访问关于它自己的线程组的信息,但是不允许访问它所在线程组的父线程组或任何其他线程组的信息。

      

代码示例:

package com.yu;

public class ThreadGroupTest {
	public static void main(String[] args) throws Exception {
		ThreadGroup tg_parent = new ThreadGroup("父线程组");
		ThreadGroup tg_son = new ThreadGroup(tg_parent, "子线程组");
		Thread t1 = new Thread(tg_son, () -> {
			System.out.println("线程名称:" + Thread.currentThread().getName());
			System.out.println("线程组名称:" + Thread.currentThread().getThreadGroup().getName());
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}, "t1线程");

		Thread t2 = new Thread(tg_parent, () -> {
			System.out.println("线程名称:" + Thread.currentThread().getName());
			System.out.println("线程组名称:" + Thread.currentThread().getThreadGroup().getName());
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}, "t2线程");

		Thread t3 = new Thread(tg_parent, () -> {
			System.out.println("线程名称:" + Thread.currentThread().getName());
			System.out.println("线程组名称:" + Thread.currentThread().getThreadGroup().getName());
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}, "t2线程");

		System.out.println("线程组名称:"+t1.getThreadGroup().getName());
		System.out.println("线程是否在给定的线程组中:"+t1.getThreadGroup().parentOf(tg_parent));
		System.out.println("线程是否在给定的线程组中:"+t1.getThreadGroup().parentOf(tg_son));
		System.out.println("线程组中活的线程数:"+tg_parent.activeCount());//是一个评估值
		System.out.println("线程组中活的线程组数:"+tg_parent.activeGroupCount());//是一个评估值
		System.out.println("线程组的最大优先级:"+t1.getThreadGroup().getMaxPriority());

	}
}

使用场景: 1 大型任务,可分成多个独立的子线程并发进行,只要其中的某个子线程完成了任务条件,就算任务完成,则调用 threadGroup.interrupt() 方法 其他的子线程就可以停止了;2 大型任务,可分成多个独立的子线程并发进行,最后等待所有的子线程执行结束然后继续往下执行。

二、ThreadFactory接口介绍

        ThreadFactory是一个接口,只有一个Thread newThread(Runnable r) 方法;

        自定义线程工厂实现类按照一定的规则像工厂流水线那样生产线程,这些线程 “ 长得都挺像的  ” ;

         自定义线程工厂实现类,可以跟踪线程池究竟在何时创建了多少线程,也可以自定义线程名称,组,优先级,甚至直接设定所有线程为守护线程。可以通过自定义线程池更加自由的设置池子里所有线程的状态。

代码示例:

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 {
		// 自定义线程工厂
		ThreadFactory threadFactoryOwner = new ThreadFactory() {

			@Override
			public Thread newThread(Runnable r) {
				Thread t = new Thread(r);
				t.setName("threadFactoryOwner-pool-" + t.getId());
				t.setDaemon(true);// 设置为守护进程
				return t;
			}
		};

		// 使用自定义的工厂创建线程池
		ExecutorService executorService = Executors.newFixedThreadPool(10, threadFactoryOwner);
		for (int i = 0; i < 10; i++) {
			executorService.submit(() -> {
				System.out.println("线程名称:" + Thread.currentThread().getName());
				System.out.println("线程线程组:" + Thread.currentThread().getThreadGroup());
				System.out.println("线程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默认的线程工厂实现类:创建线程池的时候指定线程工厂实现类,如果不指定则使用JDK默认的线程工厂。

JDK默认的线程工厂实现类

三、二者的联系

        自定义线程工厂实现类的时候可以使用指定的线程组。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值