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之外,它还会和父线程拥有相同的优先级。

猜你喜欢

转载自blog.csdn.net/ppwwp/article/details/86771872