java线程init_多线程之Thread.init()方法学习

/*** Initializes a Thread.

*

*@paramg the Thread group

*@paramtarget the object whose run() method gets called

*@paramname the name of the new Thread

*@paramstackSize the desired stack size for the new thread, or

* zero to indicate that this parameter is to be ignored.

*@paramacc the AccessControlContext to inherit, or

* AccessController.getContext() if null

*@paraminheritThreadLocals if {@codetrue}, inherit initial values for

* inheritable thread-locals from the constructing thread*/

private voidinit(ThreadGroup g, Runnable target, String name,longstackSize, AccessControlContext acc,booleaninheritThreadLocals) {//名字不可以为null,否则抛出异常

if (name == null) {throw new NullPointerException("name cannot be null");

}this.name =name;//取当前创建的线程作为该线程的父线程

Thread parent =currentThread();

SecurityManager security=System.getSecurityManager();if (g == null) {/*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*/

//该方法是一个id自增功能

tid =nextThreadID();

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值