线程组的概念

主线程&主线程组

主线程的名字是main,线程组为main

主线程内创建的子线程,默认所在的组是主线程所在的组。

package com.jdojo.threads;

public class DefaultThreadGroup {
       public static void main(String[] args) {

            // Get the current thread, which is called "main" 
            Thread t1 = Thread.currentThread();
            // Get the thread group of the main thread 
            ThreadGroup tg1 = t1.getThreadGroup();
            System.out.println("Current thread's name: " + t1.getName());  
            System.out.println("Current thread's group name: " + tg1.getName());

            // Creates a new thread. Its thread group is the same that of the main thread. 
            Thread t2 = new Thread("my new thread");
            ThreadGroup tg2 = t2.getThreadGroup();
            System.out.println("New thread's name: " + t2.getName()); 
            System.out.println("New thread's group name: " + tg2.getName());
        } 
}

 

创建线程组&指定线程的线程组

// Create a new ThreadGroup
ThreadGroup myGroup = new ThreadGroup("My Thread Group");

// Make the new thread a member of the myGroup thread group 
Thread t = new Thread(myGroup, "myThreadName");

 

线程组的方法

当前活跃线程数activeCount()

父线程组getParent()

线程数enumerate()

中断所有线程interrupt()

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值