好久没看源码了,上次写完一部分常见的集合类的源码分析之后,今天抽空看了看Thread的源码。今天也来记录分析一下。
我们都知道创建线程,有两种方案。一个是继承Thread一个是实现runnable接口。具体的方法就不写了。我们直接从Thread类分析入手。
先来看一下构造器
这是Thread提供的构造器。我们看到有我们经常用到的一个空构造器,一个带入参为runnable的构造器。
public Thread() {
init(null, null, "Thread-" + nextThreadNum(), 0);
}
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
还有其他的例如:
public Thread(Runnable target, String name) {
init(null, target, name, 0);
}
public Thread(String name) {
init(null, null, name, 0);
}
后面就不多举例了,这些构造器内部都调用一个init方法,传入4个参数。
再看一下这个inti方法:
private void init(ThreadGroup g, Runnable target, String name,
long stackSize) {
init(g, target, name, stackSize, null);
}
内部又调用了一个init。并且多传入了一个null作为入参:
private void init(ThreadGroup g, Runnable target, String name,
long stackSize, AccessControlContext acc) {
//首先判断一下这个name是否为null,这里如果null会抛出NPE。
if (name == null) {
throw new NullPointerException("name cannot be null");
}
//所以,前面的构造器使用时,如果我们使用不带name的构造器,那么thread类会给我们拼一个默认的名字“thread”+一个线程数。
this.name = name.toCharArray();
//然后继续获取当前线程的线程信息。明明变量名为parent。是将其作为要新建的线程的父线程。
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;
//嗲用native方法对新线程进行赋值优先级,赋值前进行了一些校验工作
setPriority(priority);
//thread中有一个threadlocal,这里将父线程的赋值给要创建的线程
if (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();
}
这里我们这个thread实例已经创建完成了。
后面需要调用start方法开始新线程,看一下start
/**
* Causes this thread to begin execution; the Java Virtual Machine
* calls the <code>run</code> method of this thread.
* <p>
* The result is that two threads are running concurrently: the
* current thread (which returns from the call to the
* <code>start</code> method) and the other thread (which executes its
* <code>run</code> method).
* <p>
* It is never legal to start a thread more than once.
* In particular, a thread may not be restarted once it has completed
* execution.
*
* @exception IllegalThreadStateException if the thread was already
* started.
* @see #run()
* @see #stop()
*/
public synchronized void start() {
/**
* This method is not invoked for the main method thread or "system"
* group threads created/set up by the VM. Any new functionality added
* to this method in the future may have to also be added to the VM.
*
* A zero status value corresponds to state "NEW".
*/
if (threadStatus != 0)
throw new IllegalThreadStateException();
/* Notify the group that this thread is about to be started
* so that it can be added to the group's list of threads
* and the group's unstarted count can be decremented. */
group.add(this);
boolean started = false;
try {
start0();
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then
it will be passed up the call stack */
}
}
}
private native void start0();
先校验当前线程的状态是否为new,如果不是,则抛出异常,县城已经被启动。并且将此线程加入到线程组中。然后调用native方法start0运行线程。如果最后县城没有运行成功,则调用线程组的threadStartFailed方法。
看注释理解一下,运行start时,两个线程同时执行,不过当前线程执行完start直接返回了;然后新线程回去运行我们Thread中的run方法。再看一下run:
@Override
public void run() {
if (target != null) {
target.run();
}
}
因为Thread类也实现了Runnable方法。所以这里需要实现run。
这里Thread又复合了一个Runnable对象。所以run会判断当前复合的target变量是否为空,如果不为空则使用实现Runnable接口的类中的run方法。如果使用继承Thread的方式,则使用子类重写的run方法。
大概就这么多吧。后面还会写一些比如callable,线程池,并发,锁相关的内容。如有分析不到位的地方也可以请指正。