Java
中的线程是使用Thread
类实现的,Thread
在初学Java
的时候就学过了,也在实践中用过,不过一直没从源码的角度去看过它的实现,今天从源码的角度出发,再次学习Java Thread
,愿此后对Thread
的实践更加驾轻就熟。java
相信阅读过JDK
源码的同窗都能感觉到JDK
源码中有很是详尽的注释,阅读某个类的源码应当先看看注释对它的介绍,注释原文就不贴了,如下是我对它的总结:程序员
Thread
是程序中执行的线程,Java
虚拟机容许应用程序同时容许多个执行线程安全
每一个线程都有优先级的概念,具备较高优先级的线程优先于优先级较低的线程执行less
每一个线程均可以被设置为守护线程ide
当在某个线程中运行的代码建立一个新的Thread
对象时,新的线程优先级跟建立线程一致函数
当Java
虚拟机启动的时候都会启动一个叫作main
的线程,它没有守护线程,main
线程会继续执行,直到如下状况发送学习
Runtime
类的退出方法exit
被调用而且安全管理器容许进行退出操做run
方法执行结束正常返回结果,或者run
方法抛出异常建立线程第一种方式:继承Thread
类,重写run
方法测试
//定义线程类
class PrimeThread extends Thread {
long minPrime;
PrimeThread(long minPrime) {
this.minPrime = minPrime;
}
public void run() {
// compute primes larger than minPrime
. . .
}
}
//启动线程
PrimeThread p = new PrimeThread(143);
p.start();
复制代码
建立线程第二种方式:实现Runnable
接口,重写run
方法,由于Java
的单继承限制,一般使用这种方式建立线程更加灵活this
//定义线程
class PrimeRun implements Runnable {
long minPrime;
PrimeRun(long minPrime) {
this.minPrime = minPrime;
}
public void run() {
// compute primes larger than minPrime
. . .
}
}
//启动线程
PrimeRun p = new PrimeRun(143);
new Thread(p).start();
复制代码
建立线程时能够给线程指定名字,若是没有指定,会自动为它生成名字spa
除非另有说明,不然将null
参数传递给Thread
类中的构造函数或方法将致使抛出 NullPointerException
阅读一个Java
类,先从它拥有哪些属性入手:
//线程名称,建立线程时能够指定线程的名称
private volatile String name;
//线程优先级,能够设置线程的优先级
private int priority;
//能够配置线程是否为守护线程,默认为false
private boolean daemon = false;
//最终执行线程任务的`Runnable`
private Runnable target;
//描述线程组的类
private ThreadGroup group;
//此线程的上下文ClassLoader
private ClassLoader contextClassLoader;
//全部初始化线程的数目,用于自动编号匿名线程,当没有指定线程名称时,会自动为其编号
private static int threadInitNumber;
//此线程请求的堆栈大小,若是建立者没有指定堆栈大小,则为0。, 虚拟机能够用这个数字作任何喜欢的事情。, 一些虚拟机会忽略它。
private long stackSize;
//线程id
private long tid;
//用于生成线程ID
private static long threadSeqNumber;
//线程状态
private volatile int threadStatus = 0;
//线程能够拥有的最低优先级
public final static int MIN_PRIORITY = 1;
//分配给线程的默认优先级。
public final static int NORM_PRIORITY = 5;
//线程能够拥有的最大优先级
public final static int MAX_PRIORITY = 10;
复制代码
全部的属性命名都很语义化,其实已看名称基本就猜到它是干吗的了,难度不大~~
了解了属性以后,看看Thread
实例是怎么构造的?先预览下它大体有多少个构造方法:
查看每一个构造方法内部源码,发现均调用的是名为init
的私有方法,再看init
方法有两个重载,而其核心方法以下:
/** * Initializes a Thread. * * @param g 线程组 * @param target 最终执行任务的 `run()` 方法的对象 * @param name 新线程的名称 * @param stackSize 新线程所需的堆栈大小,或者 0 表示要忽略此参数 * @param acc 要继承的AccessControlContext,若是为null,则为 AccessController.getContext() * @param 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) {
//若是安全管理器不为空
if (security != null) {
//获取SecurityManager中的线程组
g = security.getThreadGroup();
}
//若是获取的线程组仍是为空
if (g == null) {
//则使用父线程的线程组
g = parent.getThreadGroup();
}
}
//检查安全权限
g.checkAccess();
//使用安全管理器检查是否有权限
if (security != null) {
if (isCCLOverridden(getClass())) {
security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
}
}
//线程组中标记未启动的线程数+1,这里方法是同步的,防止出现线程安全问题
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);
//初始化当前线程对象的堆栈大小
this.stackSize = stackSize;
//初始化当前线程对象的线程ID,该方法是同步的,内部其实是threadSeqNumber++
tid = nextThreadID();
}
复制代码
另外一个重载init
私有方法以下,实际上内部调用的是上述init
方法:
private void init(ThreadGroup g, Runnable target, String name, long stackSize) {
init(g, target, name, stackSize, null, true);
}
复制代码
接下来看看全部构造方法:
空构造方法
public Thread() {
init(null, null, "Thread-" + nextThreadNum(), 0);
}
复制代码
内部调用的是init
第二个重载方法,参数基本都是默认值,线程名称写死为"Thread-" + nextThreadNum()
格式,nextThreadNum()
为一个同步方法,内部维护一个静态属性表示线程的初始化数量+1:
private static int threadInitNumber;
private static synchronized int nextThreadNum() {
return threadInitNumber++;
}
复制代码
自定义执行任务Runnable
对象的构造方法
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
复制代码
与第一个构造方法区别在于能够自定义Runnable
对象
自定义执行任务Runnable
对象和AccessControlContext
对象的构造方法
Thread(Runnable target, AccessControlContext acc) {
init(null, target, "Thread-" + nextThreadNum(), 0, acc, false);
}
复制代码
自定义线程组ThreadGroup
和执行任务Runnable
对象的构造方法
public Thread(ThreadGroup group, Runnable target) {
init(group, target, "Thread-" + nextThreadNum(), 0);
}
复制代码
自定义线程名称name
的构造方法
public Thread(String name) {
init(null, null, name, 0);
}
复制代码
自定义线程组ThreadGroup
和线程名称name
的构造方法
public Thread(ThreadGroup group, String name) {
init(group, null, name, 0);
}
复制代码
自定义执行任务Runnable
对象和线程名称name
的构造方法
public Thread(Runnable target, String name) {
init(null, target, name, 0);
}
复制代码
自定义线程组ThreadGroup
和线程名称name
和执行任务Runnable
对象的构造方法
public Thread(ThreadGroup group, Runnable target, String name) {
init(group, target, name, 0);
}
复制代码
所有属性都是自定义的构造方法
public Thread(ThreadGroup group, Runnable target, String name, long stackSize) {
init(group, target, name, stackSize);
}
复制代码
Thread
提供了很是灵活的重载构造方法,方便开发者自定义各类参数的Thread
对象。
这里记录一些比较常见的方法吧,对于Thread
中存在的一些本地方法,咱们暂且不用管它~
设置线程名称,该方法为同步方法,为了防止出现线程安全问题,能够手动调用Thread
的实例方法设置名称,也能够在构造Thread
时在构造方法中传入线程名称,咱们一般都是在构造参数时设置
public final synchronized void setName(String name) {
  //检查安全权限
checkAccess();
  //若是形参为空,抛出空指针异常
if (name == null) {
throw new NullPointerException("name cannot be null");
}
//给当前线程对象设置名称
this.name = name;
if (threadStatus != 0) {
setNativeName(name);
}
}
复制代码
内部直接返回当前线程对象的名称属性
public final String getName() {
return name;
}
复制代码
public synchronized void start() {
//若是不是刚建立的线程,抛出异常
if (threadStatus != 0)
throw new IllegalThreadStateException();
//通知线程组,当前线程即将启动,线程组当前启动线程数+1,未启动线程数-1
group.add(this);
//启动标识
boolean started = false;
try {
//直接调用本地方法启动线程
start0();
//设置启动标识为启动成功
started = true;
} finally {
try {
//若是启动呢失败
if (!started) {
//线程组内部移除当前启动的线程数量-1,同时启动失败的线程数量+1
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then it will be passed up the call stack */
}
}
}
复制代码
咱们正常的启动线程都是调用Thread
的start()
方法,而后Java
虚拟机内部会去调用Thred
的run
方法,能够看到Thread
类也是实现Runnable
接口,重写了run
方法的:
@Override
public void run() {
//当前执行任务的Runnable对象不为空,则调用其run方法
if (target != null) {
target.run();
}
}
复制代码
Thread
的两种使用方式:
Thread
类,重写run
方法,那么此时是直接执行run
方法的逻辑,不会使用target.run();
Runnable
接口,重写run
方法,由于Java
的单继承限制,一般使用这种方式建立线程更加灵活,这里真正的执行逻辑就会交给自定义Runnable
去实现本质操做是设置daemon
属性
public final void setDaemon(boolean on) {
//检查是否有安全权限
checkAccess();
//本地方法,测试此线程是否存活。, 若是一个线程已经启动而且还没有死亡,则该线程处于活动状态
if (isAlive()) {
//若是线程先启动后再设置守护线程,将抛出异常
throw new IllegalThreadStateException();
}
//设置当前守护线程属性
daemon = on;
}
复制代码
public final boolean isDaemon() {
//直接返回当前对象的守护线程属性
return daemon;
}
复制代码
先来个线程状态图:
获取线程状态:
public State getState() {
//由虚拟机实现,获取当前线程的状态
return sun.misc.VM.toThreadState(threadStatus);
}
复制代码
线程状态主要由内部枚举类State
组成:
public enum State {
NEW,
RUNNABLE,
BLOCKED,
WAITING,
TIMED_WAITING,
TERMINATED;
}
复制代码
synchronized
同步块,就会进入此状态,此时线程暂停执行,直到得到请求的锁这是一个静态的本地方法,使当前执行的线程休眠暂停执行 millis
毫秒,当休眠被中断时会抛出InterruptedException
中断异常
/** * Causes the currently executing thread to sleep (temporarily cease * execution) for the specified number of milliseconds, subject to * the precision and accuracy of system timers and schedulers. The thread * does not lose ownership of any monitors. * * @param millis * the length of time to sleep in milliseconds * * @throws IllegalArgumentException * if the value of {@code millis} is negative * * @throws InterruptedException * if any thread has interrupted the current thread. The * <i>interrupted status</i> of the current thread is * cleared when this exception is thrown. */
public static native void sleep(long millis) throws InterruptedException;
复制代码
本地方法,测试此线程是否存活。 若是一个线程已经启动而且还没有死亡,则该线程处于活动状态。
/** * Tests if this thread is alive. A thread is alive if it has * been started and has not yet died. * * @return <code>true</code> if this thread is alive; * <code>false</code> otherwise. */
public final native boolean isAlive();
复制代码
/** * Changes the priority of this thread. * <p> * First the <code>checkAccess</code> method of this thread is called * with no arguments. This may result in throwing a * <code>SecurityException</code>. * <p> * Otherwise, the priority of this thread is set to the smaller of * the specified <code>newPriority</code> and the maximum permitted * priority of the thread's thread group. * * @param newPriority priority to set this thread to * @exception IllegalArgumentException If the priority is not in the * range <code>MIN_PRIORITY</code> to * <code>MAX_PRIORITY</code>. * @exception SecurityException if the current thread cannot modify * this thread. * @see #getPriority * @see #checkAccess() * @see #getThreadGroup() * @see #MAX_PRIORITY * @see #MIN_PRIORITY * @see ThreadGroup#getMaxPriority() */
public final void setPriority(int newPriority) {
//线程组
ThreadGroup g;
//检查安全权限
checkAccess();
//检查优先级形参范围
if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
throw new IllegalArgumentException();
}
if((g = getThreadGroup()) != null) {
//若是优先级形参大于线程组最大线程最大优先级
if (newPriority > g.getMaxPriority()) {
//则使用线程组的优先级数据
newPriority = g.getMaxPriority();
}
//调用本地设置线程优先级方法
setPriority0(priority = newPriority);
}
}
复制代码
有一个stop()
实例方法能够强制终止线程,不过这个方法由于太过于暴力,已经被标记为过期方法,不建议程序员再使用,由于强制终止线程会致使数据不一致的问题。
这里关于线程中断的方法涉及三个:
//实例方法,通知线程中断,设置标志位
public void interrupt(){}
//静态方法,检查当前线程的中断状态,同时会清除当前线程的中断标志位状态
public static boolean interrupted(){}
//实例方法,检查当前线程是否被中断,实际上是检查中断标志位
public boolean isInterrupted(){}
复制代码
interrupt() 方法解析
/** * Interrupts this thread. * * <p> Unless the current thread is interrupting itself, which is * always permitted, the {@link #checkAccess() checkAccess} method * of this thread is invoked, which may cause a {@link * SecurityException} to be thrown. * * <p> If this thread is blocked in an invocation of the {@link * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link * Object#wait(long, int) wait(long, int)} methods of the {@link Object} * class, or of the {@link #join()}, {@link #join(long)}, {@link * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)}, * methods of this class, then its interrupt status will be cleared and it * will receive an {@link InterruptedException}. * * <p> If this thread is blocked in an I/O operation upon an {@link * java.nio.channels.InterruptibleChannel InterruptibleChannel} * then the channel will be closed, the thread's interrupt * status will be set, and the thread will receive a {@link * java.nio.channels.ClosedByInterruptException}. * * <p> If this thread is blocked in a {@link java.nio.channels.Selector} * then the thread's interrupt status will be set and it will return * immediately from the selection operation, possibly with a non-zero * value, just as if the selector's {@link * java.nio.channels.Selector#wakeup wakeup} method were invoked. * * <p> If none of the previous conditions hold then this thread's interrupt * status will be set. </p> * * <p> Interrupting a thread that is not alive need not have any effect. * * @throws SecurityException * if the current thread cannot modify this thread * * @revised 6.0 * @spec JSR-51 */
public void interrupt() {
//检查是不是自身调用
if (this != Thread.currentThread())
//检查安全权限,这可能致使抛出{@link * SecurityException}。
checkAccess();
//同步代码块
synchronized (blockerLock) {
Interruptible b = blocker;
//检查是不是阻塞线程调用
if (b != null) {
//设置线程中断标志位
interrupt0();
//此时抛出异常,将中断标志位设置为false,此时咱们正常会捕获该异常,从新设置中断标志位
b.interrupt(this);
return;
}
}
//如无心外,则正常设置中断标志位
interrupt0();
}
复制代码
SecurityException
wait
,join
,sleep
等方法,会使当前线程进入阻塞状态,此时有可能发生InterruptedException
异常检查线程是否被中断:
/** * Tests whether this thread has been interrupted. The <i>interrupted * status</i> of the thread is unaffected by this method. 测试此线程是否已被中断。, 线程的<i>中断*状态</ i>不受此方法的影响。 * * <p>A thread interruption ignored because a thread was not alive * at the time of the interrupt will be reflected by this method * returning false. * * @return <code>true</code> if this thread has been interrupted; * <code>false</code> otherwise. * @see #interrupted() * @revised 6.0 */
public boolean isInterrupted() {
return isInterrupted(false);
}
复制代码
静态方法,会清空当前线程的中断标志位:
/** *测试当前线程是否已被中断。, 此方法清除线程的* <i>中断状态</ i>。, 换句话说,若是要连续两次调用此方法,则* second调用将返回false(除非当前线程再次被中断,在第一次调用已清除其中断的*状态 以后且在第二次调用已检查以前), 它) * * <p>A thread interruption ignored because a thread was not alive * at the time of the interrupt will be reflected by this method * returning false. * * @return <code>true</code> if the current thread has been interrupted; * <code>false</code> otherwise. * @see #isInterrupted() * @revised 6.0 */
public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
复制代码
记录本身阅读Thread
类源码的一些思考,不过对于其中用到的不少本地方法只能望而却步,还有一些代码没有看明白,暂且先这样吧,若是有不足之处,请留言告知我,谢谢!后续会在实践中对Thread
作出更多总结记录。
因为篇幅较长,暂且先记录这些吧,后续会不按期更新原创文章,欢迎关注公众号 「张少林同窗」!