1线程创建
一个新构造的线程对象是由其parent线程来进行空间分配的,而child线程继承了parent是否为Daemon、优先级和加载资源的contextClassLoader以及可继承的ThreadLocal,同时还会分配一个唯一的(sync)ID来标识这个child线程。至此,一个能够运行的线程对象就初始化好了,在堆内存中等待着运行。
下面我们进行源码阅读:
当我们new Thread的时候,会调用init方法,
public Thread() {
init(null, null, "Thread-" + nextThreadNum(), 0);
}
init方法点进去
private void init(ThreadGroup g, Runnable target, String name,
long stackSize) {
init(g, target, name, stackSize, null, true);
}
此时g为null,target为null,name是"Thread-" + nextThreadNum(),stackSize是0,acc是null,inheritThreadLocals是true。
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) {
/* 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();
}
2源码剖析
接下来逐行分析:
if (name == null) {
throw new NullPointerException("name cannot be null");
}
this.name = name;
如果当前线程name为null,直接抛出错误,否则继续执行,把传进来的线程名称赋值给当前线程name
Thread parent = currentThread();
currentThread()方法是个native方法,大致意思是那个线程创建的这个线程,父线程就是谁。比如你在main函数里创建了一个线程,那么这个线程的父线程就是main线程。
SecurityManager security = System.getSecurityManager();
if (g == null) {
if (security != null) {
g = security.getThreadGroup();
}
if (g == null) {
g = parent.getThreadGroup();
}
}
SecurityManager含义:当运行未知的Java程序的时候,该程序可能有恶意代码(删除系统文件、重启系统等),为了防止运行恶意代码对系统产生影响,需要对运行的代码的权限进行控制,这时候就要启用Java安全管理器。
这里的security主要是为了防止g为null,就是给当前线程找一个线程组,从SecurityManager安全管理器中拿到group。如果此时SecurityManager为null,那就只能从父类里面拿到线group。
尊重线程初始化穿入的threadgroup;次选System security mananger 的 threadgroup;再次选 parent的 threadgroup。
g.checkAccess();
判断当前线程组是不是有权限
if (security != null) {
if (isCCLOverridden(getClass())) {
security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
}
}
判断子类和实现的接口是否有权限
g.addUnstarted();
NEW状态的线程,会添加到当前线程的threadgroup
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();
新的线程的属性(是否为守护线程、优先级)依赖于 父类线程。contextClassLoader类加载器也继承父类加载器。继承的inheritedAccessControlContext用于根据其封装的上下文做出系统资源访问决策
this.target = target;
setPriority(priority);
设置当前的Runnable对象和优先级
if (inheritThreadLocals && parent.inheritableThreadLocals != null)
this.inheritableThreadLocals =
ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
这里有一个inheritableThreadLocals,是为了子类的线程也能继承父类线程里的ThreadLocal,所以才有了一个inheritableThreadLocals,就是为了传递。
this.stackSize = stackSize;
tid = nextThreadID();
设置新线程栈大小,可以理解为开辟一块内存空间。 nextThreadID()是分配一个线程tid,并且这个id是synchronize修饰的,保证线程tid的唯一性和线程安全。
private static synchronized long nextThreadID() {
return ++threadSeqNumber;
}
这章就到这里结束,对线程的创建进行认识