java thread构造方法_Java编程在Thread构造函数中,如何编写Threadinit方法的源码?...

在Thread的构造函数中,可以显式地指定线程的Group,也就是ThreadGroup。接着下面介绍Thread init方法的源码:

SecurityManager security=System.getSecurityManager();if(g==null){

/*Determine if it's an applet or not*/

/*If there is a security manager,ask the security man:what to do.*/

if(security!=null){

g=security.getThreadGroup();

/*If the security doesn't have a strong opinion of the use the parent thread group.*/

if(g==null){

g=parent.getThreadGroup();

}

}

98f6c944f2da42e3d8613f2cd8c31919.png

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

ThreadConstruction.java package com.wangwenjun.concurrent.chapter02;

public class ThreadConstruction

{

public static void main(String[]args)

//①

Thread t1=new Thread("t1");

1/②

ThreadGroup group=new ThreadGroup("TestGroup");

1/③

Thread t2=new Thread(group,"t2");

ThreadGroup mainThreadGroup=Thread.currentThread()

System.out.println("Main thread belong group:"+ma;

System.out.println("tl and main belong the same grot ;

System.out.println("t2 thread group not belong main ;

System.out.println("t2 thread group belong main Test;

}

}

c536be738896ece5ec97c61aebe9a022.png

在默认设置中,当然除了子线程会和父线程同属于一个Group之外,它还会和父线程拥有同样的优先级。以上就是Java编程在Thread构造函数中,编写Thread init方法源码的编程。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值