java高并发总结7--java Thread与ThreadGroup初探

在Thread的构造函数中,可以显式地指定现成的Group,也就是ThreadGroup。
接下来我们看一下Thread的init方法源码:

private void init(ThreadGroup g, Runnable target, String name,
                      long stackSize, AccessControlContext acc,
                      boolean inheritThreadLocals) {
        if (name == null) {
            throw new NullPointerException("name cannot be null");
        }

        this.name = name;

        Thread parent = currentThread();
        SecurityManager security = System.getSecurityManager();
        if (g == null) {             //如果参数没有传入group
            /* Determine if it's an applet or not */

            /* If there is a security manager, ask the security manager
               what to do. */
            if (security != null) {
                g = security.getThreadGroup();
            }

            /* If the security doesn't have a strong opinion of the matter
               use the parent thread group. */
            if (g == null) {
                g = parent.getThreadGroup();
            }
        }

        /* checkAccess regardless of whether or not threadgroup is
           explicitly passed in. */
        g.checkAccess();

        /*
         * Do we have the required permissions?
         */
        if (security != null) {
            if (isCCLOverridden(getClass())) {
                security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
            }
        }

        g.addUnstarted();

        this.group = g;
        this.daemon = parent.isDaemon();
        this.priority = parent.getPriority();
        if (security == null || isCCLOverridden(parent.getClass()))
            this.contextClassLoader = parent.getContextClassLoader();
        else
            this.contextClassLoader = parent.contextClassLoader;
        this.inheritedAccessControlContext =
                acc != null ? acc : AccessController.getContext();
        this.target = target;
        setPriority(priority);
        if (inheritThreadLocals && parent.inheritableThreadLocals != null)
            this.inheritableThreadLocals =
                ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
        /* Stash the specified stack size in case the VM cares */
        this.stackSize = stackSize;

        /* Set thread ID */
        tid = nextThreadID();
    }
    if (g == null) {             //如果参数没有传入group

通过对源码进行分析,我们可以看出,如果在构造Thread的时候没有显示地指定一个ThreadGroup,那么子线程将会被加入父线程所在的线程组,下面我们来写一个简单的代码测试:

package lambda;

public class ThreadConstruction {
	public static void main(String[] args) {
		Thread t1 = new Thread("t1");
		
		ThreadGroup g = new ThreadGroup("testGroup");
		
		Thread t2 = new Thread(g,"t2");
		
		ThreadGroup mainThreadGroup = Thread.currentThread().getThreadGroup();
		
		System.out.println("Main thread belong the same group:"+mainThreadGroup.getName());
		
		System.out.println("t1 and main belong the same group:"+(mainThreadGroup == t1.getThreadGroup()));
		
		System.out.println("t2 thread group not belong main group:"+(mainThreadGroup == t2.getThreadGroup()));
		
		System.out.println("t2 thread group not belong main testgroup:"+(g == t2.getThreadGroup()));
	}
}

测试结果

Main thread belong the same group:main
t1 and main belong the same group:true
t2 thread group not belong main group:false
t2 thread group not belong main testgroup:true

听过对Thread源码的分析和我们自己的测试结果可以得出如下结论:

  1. main线程所在的ThreadGroup成为main
  2. 构造一个线程的时候如果没有显式地指定ThreadGroup,那么它将会和父线程同属于一个ThreadGroup。

在默认设置中,当然除了子线程和府县丞同属于一个group之外,它还会和父线程拥有相同的优先级。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值