欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java高并发之线程组详解

程序员文章站 2024-01-02 19:00:58
目录线程组创建线程关联线程组为线程组指定父线程组根线程组批量停止线程总结线程组我们可以把线程归属到某个线程组中,线程组可以包含多个线程以及线程组,线程和线程组组成了父子关系,是个树形结构,如下图:使用...

线程组

我们可以把线程归属到某个线程组中,线程组可以包含多个线程以及线程组,线程和线程组组成了父子关系,是个树形结构,如下图:

java高并发之线程组详解

使用线程组可以方便管理线程,线程组提供了一些方法方便方便我们管理线程。

创建线程关联线程组

创建线程的时候,可以给线程指定一个线程组,代码如下:

package com.itsoku.chat02;	
import java.util.concurrent.timeunit;	
/**	
 * <b>description</b>:<br>	
 * <b>time</b>:2019/7/13 17:53 <br>	
 * <b>author</b>:微信公众号:路人甲java,专注于java技术分享(带你玩转 爬虫、分布式事务、异步消息服务、任务调度、分库分表、大数据等),喜欢请关注!	
 */	
public class demo1 {	
    public static class r1 implements runnable {	
        @override	
        public void run() {	
            system.out.println("threadname:" + thread.currentthread().getname());	
            try {	
                timeunit.seconds.sleep(3);	
            } catch (interruptedexception e) {	
                e.printstacktrace();	
            }	
        }	
    }	
    public static void main(string[] args) throws interruptedexception {	
        threadgroup threadgroup = new threadgroup("thread-group-1");	
        thread t1 = new thread(threadgroup, new r1(), "t1");	
        thread t2 = new thread(threadgroup, new r1(), "t2");	
        t1.start();	
        t2.start();	
        timeunit.seconds.sleep(1);	
        system.out.println("活动线程数:" + threadgroup.activecount());	
        system.out.println("活动线程组:" + threadgroup.activegroupcount());	
        system.out.println("线程组名称:" + threadgroup.getname());	
    }	
}

输出结果:

threadname:t1
threadname:t2
活动线程数:2
活动线程组:0
线程组名称:thread-group-1

activecount()方法可以返回线程组中的所有活动线程数,包含下面的所有子孙节点的线程,由于线程组中的线程是动态变化的,这个值只能是一个估算值。

为线程组指定父线程组

创建线程组的时候,可以给其指定一个父线程组,也可以不指定,如果不指定父线程组,则父线程组为当前线程的线程组,java api有2个常用的构造方法用来创建线程组:

public threadgroup(string name) 	
public threadgroup(threadgroup parent, string name)

第一个构造方法未指定父线程组,看一下内部的实现:

public threadgroup(string name) {	
        this(thread.currentthread().getthreadgroup(), name);	
    }

系统自动获取当前线程的线程组作为默认父线程组。

上一段示例代码:

package com.itsoku.chat02;	
import java.util.concurrent.timeunit;	
/**	
 * <b>description</b>:<br>	
 * <b>time</b>:2019/7/13 17:53 <br>	
 * <b>author</b>:微信公众号:路人甲java,专注于java技术分享(带你玩转 爬虫、分布式事务、异步消息服务、任务调度、分库分表、大数据等),喜欢请关注!	
 */	
public class demo2 {	
    public static class r1 implements runnable {	
        @override	
        public void run() {	
            thread thread = thread.currentthread();	
            system.out.println("所属线程组:" + thread.getthreadgroup().getname() + ",线程名称:" + thread.getname());	
            try {	
                timeunit.seconds.sleep(3);	
            } catch (interruptedexception e) {	
                e.printstacktrace();	
            }	
        }	
    }	
    public static void main(string[] args) throws interruptedexception {	
        threadgroup threadgroup1 = new threadgroup("thread-group-1");	
        thread t1 = new thread(threadgroup1, new r1(), "t1");	
        thread t2 = new thread(threadgroup1, new r1(), "t2");	
        t1.start();	
        t2.start();	
        timeunit.seconds.sleep(1);	
        system.out.println("threadgroup1活动线程数:" + threadgroup1.activecount());	
        system.out.println("threadgroup1活动线程组:" + threadgroup1.activegroupcount());	
        system.out.println("threadgroup1线程组名称:" + threadgroup1.getname());	
        system.out.println("threadgroup1父线程组名称:" + threadgroup1.getparent().getname());	
        system.out.println("----------------------");	
        threadgroup threadgroup2 = new threadgroup(threadgroup1, "thread-group-2");	
        thread t3 = new thread(threadgroup2, new r1(), "t3");	
        thread t4 = new thread(threadgroup2, new r1(), "t4");	
        t3.start();	
        t4.start();	
        timeunit.seconds.sleep(1);	
        system.out.println("threadgroup2活动线程数:" + threadgroup2.activecount());	
        system.out.println("threadgroup2活动线程组:" + threadgroup2.activegroupcount());	
        system.out.println("threadgroup2线程组名称:" + threadgroup2.getname());	
        system.out.println("threadgroup2父线程组名称:" + threadgroup2.getparent().getname());	
        system.out.println("----------------------");	
        system.out.println("threadgroup1活动线程数:" + threadgroup1.activecount());	
        system.out.println("threadgroup1活动线程组:" + threadgroup1.activegroupcount());	
        system.out.println("----------------------");	
        threadgroup1.list();	
    }	
}

输出结果:

所属线程组:thread-group-1,线程名称:t1
所属线程组:thread-group-1,线程名称:t2
threadgroup1活动线程数:2
threadgroup1活动线程组:0
threadgroup1线程组名称:thread-group-1
threadgroup1父线程组名称:main
----------------------
所属线程组:thread-group-2,线程名称:t4
所属线程组:thread-group-2,线程名称:t3
threadgroup2活动线程数:2
threadgroup2活动线程组:0
threadgroup2线程组名称:thread-group-2
threadgroup2父线程组名称:thread-group-1
----------------------
threadgroup1活动线程数:4
threadgroup1活动线程组:1
----------------------
java.lang.threadgroup[name=thread-group-1,maxpri=10]
thread[t1,5,thread-group-1]
thread[t2,5,thread-group-1]
java.lang.threadgroup[name=thread-group-2,maxpri=10]
thread[t3,5,thread-group-2]
thread[t4,5,thread-group-2]

代码解释:

1.threadgroup1未指定父线程组,系统获取了主线程的线程组作为threadgroup1的父线程组,输出结果中是:main

2.threadgroup1为threadgroup2的父线程组

3.threadgroup1活动线程数为4,包含了threadgroup1线程组中的t1、t2,以及子线程组threadgroup2中的t3、t4

4.线程组的list()方法,将线程组中的所有子孙节点信息输出到控制台,用于调试使用

根线程组

获取根线程组

package com.itsoku.chat02;	
/**	
 * <b>description</b>:<br>	
 * <b>time</b>:2019/7/13 17:53 <br>	
 * <b>author</b>:微信公众号:路人甲java,专注于java技术分享(带你玩转 爬虫、分布式事务、异步消息服务、任务调度、分库分表、大数据等),喜欢请关注!	
 */	
public class demo3 {	
    public static void main(string[] args) {	
        system.out.println(thread.currentthread());	
        system.out.println(thread.currentthread().getthreadgroup());	
        system.out.println(thread.currentthread().getthreadgroup().getparent());	
        system.out.println(thread.currentthread().getthreadgroup().getparent().getparent());	
    }	
}

运行上面代码,输出:

thread[main,5,main]
java.lang.threadgroup[name=main,maxpri=10]
java.lang.threadgroup[name=system,maxpri=10]
null

从上面代码可以看出:

1.主线程的线程组为main

2.根线程组为system

看一下threadgroup的源码:

private threadgroup() {     // called from c code	
        this.name = "system";	
        this.maxpriority = thread.max_priority;	
        this.parent = null;	
    }

发现threadgroup默认构造方法是private的,是由c调用的,创建的正是system线程组。

批量停止线程

调用线程组interrupt(),会将线程组树下的所有子孙线程中断标志置为true,可以用来批量中断线程。

示例代码:

package com.itsoku.chat02;	
import java.util.concurrent.timeunit;	
/**	
 * <b>description</b>:<br>	
 * <b>time</b>:2019/7/13 17:53 <br>	
 * <b>author</b>:微信公众号:路人甲java,专注于java技术分享(带你玩转 爬虫、分布式事务、异步消息服务、任务调度、分库分表、大数据等),喜欢请关注!	
 */	
public class demo4 {	
    public static class r1 implements runnable {	
        @override	
        public void run() {	
            thread thread = thread.currentthread();	
            system.out.println("所属线程组:" + thread.getthreadgroup().getname() + ",线程名称:" + thread.getname());	
            while (!thread.isinterrupted()) {	
                ;	
            }	
            system.out.println("线程:" + thread.getname() + "停止了!");	
        }	
    }	
    public static void main(string[] args) throws interruptedexception {	
        threadgroup threadgroup1 = new threadgroup("thread-group-1");	
        thread t1 = new thread(threadgroup1, new r1(), "t1");	
        thread t2 = new thread(threadgroup1, new r1(), "t2");	
        t1.start();	
        t2.start();	
        threadgroup threadgroup2 = new threadgroup(threadgroup1, "thread-group-2");	
        thread t3 = new thread(threadgroup2, new r1(), "t3");	
        thread t4 = new thread(threadgroup2, new r1(), "t4");	
        t3.start();	
        t4.start();	
        timeunit.seconds.sleep(1);	
        system.out.println("-----------threadgroup1信息-----------");	
        threadgroup1.list();	
        system.out.println("----------------------");	
        system.out.println("停止线程组:" + threadgroup1.getname() + "中的所有子孙线程");	
        threadgroup1.interrupt();	
        timeunit.seconds.sleep(2);	
        system.out.println("----------threadgroup1停止后,输出信息------------");	
        threadgroup1.list();	
    }	
}

输出:

所属线程组:thread-group-1,线程名称:t1
所属线程组:thread-group-1,线程名称:t2
所属线程组:thread-group-2,线程名称:t3
所属线程组:thread-group-2,线程名称:t4
-----------threadgroup1信息-----------
java.lang.threadgroup[name=thread-group-1,maxpri=10]
thread[t1,5,thread-group-1]
thread[t2,5,thread-group-1]
java.lang.threadgroup[name=thread-group-2,maxpri=10]
thread[t3,5,thread-group-2]
thread[t4,5,thread-group-2]
----------------------
停止线程组:thread-group-1中的所有子孙线程
线程:t4停止了!
线程:t2停止了!
线程:t1停止了!
线程:t3停止了!
----------threadgroup1停止后,输出信息------------
java.lang.threadgroup[name=thread-group-1,maxpri=10]
java.lang.threadgroup[name=thread-group-2,maxpri=10]

停止线程之后,通过list()方法可以看出输出的信息中不包含已结束的线程了。

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注的更多内容!

上一篇:

下一篇: