Java源码阅读-Thread
简介
Thread是Java多线程实现的核心类,该类的继承结构比较简单,仅仅实现了Runable接口。Runable接口是一个功能性接口,只有一个run()方法。实现该接口的类必须实现该方法,表明自己具有可执行的能力。
构造方法
要想了解一个类,从他的构造方法入手往往会更加容易。
public Thread() {
init(null, null, "Thread-" + nextThreadNum(), 0);
}
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
Thread(Runnable target, AccessControlContext acc) {
init(null, target, "Thread-" + nextThreadNum(), 0, acc, false);
}
public Thread(ThreadGroup group, Runnable target) {
init(group, target, "Thread-" + nextThreadNum(), 0);
}
public Thread(String name) {
init(null, null, name, 0);
}
public Thread(ThreadGroup group, String name) {
init(group, null, name, 0);
}
public Thread(Runnable target, String name) {
init(null, target, name, 0);
}
public Thread(ThreadGroup group, Runnable target, String name) {
init(group, target, name, 0);
}
可以看到Thread类的众多构造方法都无一例外的调用了init方法,所以我们就直接去到init方法做参数以及初始化的分析会比较方便一点。
/**
* 初始化一个线程对象,初始化完成后的线程状态处于新建状态。
* 线程的生命周期将会在下面getState()方法中进一步分析
*
* @param g 所属的线程组(ThreadGroup类会在单独文章中分析)。
* @param target 实现了Runable接口的目标对象,如果不为空,
* 该对象的run方法将会在线程启动后被执行
* @param name the 线程名字,如果我们在调用构造方法时没有指定线程名,
* 则会在构造方法中自动生成一个"Thread-${threadNum}"模式线程名。
* threadNum是一个自增的线程计数器变量。
* @param stackSize 为该线程分配栈空间的大小,如果设置为零则虚拟机会自动忽略该参数
* @param acc 资源接入控制的上下文。(AccessControlContext会在单独的文章中分析)
* @param inheritThreadLocals 是否继承父线程的ThreadLocal资源,
* 设置为true时子线程可读取到父线程的ThreadLocal资源。
* 绝大多是情况该值都是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();
// 线程组如果为null,则初始化线程组:1、从安全管理器取。2、继承父线程的线程组
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;
// 设置是否为守护线程,默认继承父线程的,后续可通过setDaemon方法修改
this.daemon = parent.isDaemon();
// 继承父线程的优先级,后续可通过setPriority方法修改
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();
}
方法
- currentThread()
/** * 获取当前线程的引用 */ public static native Thread currentThread();
- yield()
/** * 建议当前线程让出cpu占用,仅仅是建议 */ public static native void yield();
- sleep()
/** * 使当前线程休眠millis毫秒,但并不会释放占用的锁 */ public static native void sleep(long millis) throws InterruptedException; /** * 使当前线程休眠millis毫秒,nanos纳秒。事实上对于纳秒并不准去,具体可看下面的实现 */ public static void sleep(long millis, int nanos) throws InterruptedException { if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (nanos < 0 || nanos > 999999) { throw new IllegalArgumentException( "nanosecond timeout value out of range"); } // 纳秒指定的时间大于等于0.5毫秒则取为1毫秒,小于则舍弃 if (nanos >= 500000 || (nanos != 0 && millis == 0)) { millis++; } sleep(millis); }
- start()
/** * 启动一个线程,调用该方法后线程将会从新建状态变为就绪状态, * 变为就绪状态后将会等待系统调度,有虚拟机调用他的run()方法 */ 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". * 只有新建状态的线程才能被调用start方法 */ 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();
- run()
/** * 如果该线程是使用含有Runable参数的构造方法创建的,则会最终调用目标对象的run方法 * * 如果该线程是通过继承Thread类并重写run方法的方式创建,则直接执行重写的方法 */ @Override public void run() { if (target != null) { target.run(); } }
- exit()
/** * 该方法会在线程退出前被虚拟机调用,用以必要的清理工作。 * 我们使用该方法的唯一方法重写该方法 */ private void exit() { if (group != null) { group.threadTerminated(this); group = null; } /* Aggressively null out all reference fields: see bug 4006245 */ target = null; /* Speed the release of some of these resources */ threadLocals = null; inheritableThreadLocals = null; inheritedAccessControlContext = null; blocker = null; uncaughtExceptionHandler = null; }
- stop() —— 已废弃,不再介绍
** * Forces the thread to stop executing. * <p> * If there is a security manager installed, its <code>checkAccess</code> * method is called with <code>this</code> * as its argument. This may result in a * <code>SecurityException</code> being raised (in the current thread). * <p> * If this thread is different from the current thread (that is, the current * thread is trying to stop a thread other than itself), the * security manager's <code>checkPermission</code> method (with a * <code>RuntimePermission("stopThread")</code> argument) is called in * addition. * Again, this may result in throwing a * <code>SecurityException</code> (in the current thread). * <p> * The thread represented by this thread is forced to stop whatever * it is doing abnormally and to throw a newly created * <code>ThreadDeath</code> object as an exception. * <p> * It is permitted to stop a thread that has not yet been started. * If the thread is eventually started, it immediately terminates. * <p> * An application should not normally try to catch * <code>ThreadDeath</code> unless it must do some extraordinary * cleanup operation (note that the throwing of * <code>ThreadDeath</code> causes <code>finally</code> clauses of * <code>try</code> statements to be executed before the thread * officially dies). If a <code>catch</code> clause catches a * <code>ThreadDeath</code> object, it is important to rethrow the * object so that the thread actually dies. * <p> * The top-level error handler that reacts to otherwise uncaught * exceptions does not print out a message or otherwise notify the * application if the uncaught exception is an instance of * <code>ThreadDeath</code>. * * @exception SecurityException if the current thread cannot * modify this thread. * @see #interrupt() * @see #checkAccess() * @see #run() * @see #start() * @see ThreadDeath * @see ThreadGroup#uncaughtException(Thread,Throwable) * @see SecurityManager#checkAccess(Thread) * @see SecurityManager#checkPermission * @deprecated This method is inherently unsafe. Stopping a thread with * Thread.stop causes it to unlock all of the monitors that it * has locked (as a natural consequence of the unchecked * <code>ThreadDeath</code> exception propagating up the stack). If * any of the objects previously protected by these monitors were in * an inconsistent state, the damaged objects become visible to * other threads, potentially resulting in arbitrary behavior. Many * uses of <code>stop</code> should be replaced by code that simply * modifies some variable to indicate that the target thread should * stop running. The target thread should check this variable * regularly, and return from its run method in an orderly fashion * if the variable indicates that it is to stop running. If the * target thread waits for long periods (on a condition variable, * for example), the <code>interrupt</code> method should be used to * interrupt the wait. * For more information, see * <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. */ @Deprecated public final void stop() { SecurityManager security = System.getSecurityManager(); if (security != null) { checkAccess(); if (this != Thread.currentThread()) { security.checkPermission(SecurityConstants.STOP_THREAD_PERMISSION); } } // A zero status value corresponds to "NEW", it can't change to // not-NEW because we hold the lock. if (threadStatus != 0) { resume(); // Wake up thread if it was suspended; no-op otherwise } // The VM can handle all thread states stop0(new ThreadDeath()); } /** * Throws {@code UnsupportedOperationException}. * * @param obj ignored * * @deprecated This method was originally designed to force a thread to stop * and throw a given {@code Throwable} as an exception. It was * inherently unsafe (see {@link #stop()} for details), and furthermore * could be used to generate exceptions that the target thread was * not prepared to handle. * For more information, see * <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. */ @Deprecated public final synchronized void stop(Throwable obj) { throw new UnsupportedOperationException(); }
- interrupt()、interrupted()、isInterrupted()
/** * 中断该线程,中断并不是使该线程停止执行,只是设置一个中断标志,用于线程的通信。 * 下面是一个使用中断进行线程通信小demo: * Thread thread = new Thred(){ * public void run() { * if (Thread.interrupted()) { * System.out.println("线程已被中断,执行中断逻辑......"); * } * System.out.println("线程未被中断,正常执行......"); * } * }; * thread.start(); * thread.interrupt(); * * 输出结果既有可能是"线程已被中断,执行中断逻辑......", * 也有可能是"线程未被中断,正常执行......",取决于调度情况。 * * 如果线程处于阻塞状态,则会抛出 InterruptedException。 */ public void interrupt() { if (this != Thread.currentThread()) checkAccess(); synchronized (blockerLock) { Interruptible b = blocker; if (b != null) { interrupt0(); // 仅仅设置一个标志位 b.interrupt(this); return; } } interrupt0(); } private native void interrupt0(); /** * 查看当前进程是否已被中断,并清除中断标志。 * 也就是说如果当前线程处于中断状态,连续两次调用该方法将会分别返回true、false */ public static boolean interrupted() { return currentThread().isInterrupted(true); } /** * 查看当前进程是否已被中断,但不清除中断标志。 */ public boolean isInterrupted() { return isInterrupted(false); } /** * 上面两个放的具体实现 */ private native boolean isInterrupted(boolean ClearInterrupted);
- destory() —— 已废弃,不再介绍
/** * Throws {@link NoSuchMethodError}. * * @deprecated This method was originally designed to destroy this * thread without any cleanup. Any monitors it held would have * remained locked. However, the method was never implemented. * If if were to be implemented, it would be deadlock-prone in * much the manner of {@link #suspend}. If the target thread held * a lock protecting a critical system resource when it was * destroyed, no thread could ever access this resource again. * If another thread ever attempted to lock this resource, deadlock * would result. Such deadlocks typically manifest themselves as * "frozen" processes. For more information, see * <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html"> * Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. * @throws NoSuchMethodError always */ @Deprecated public void destroy() { throw new NoSuchMethodError(); }
- isAlive()
/** * 验证线程是否处于活跃状态。活跃状态:已经调用了start方法且执行完毕 */ public final native boolean isAlive();
- suspend()、resume() —— 已废弃,不再介绍
/** * Suspends 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>(in the current thread). * <p> * If the thread is alive, it is suspended and makes no further * progress unless and until it is resumed. * * @exception SecurityException if the current thread cannot modify * this thread. * @see #checkAccess * @deprecated This method has been deprecated, as it is * inherently deadlock-prone. If the target thread holds a lock on the * monitor protecting a critical system resource when it is suspended, no * thread can access this resource until the target thread is resumed. If * the thread that would resume the target thread attempts to lock this * monitor prior to calling <code>resume</code>, deadlock results. Such * deadlocks typically manifest themselves as "frozen" processes. * For more information, see * <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. */ @Deprecated public final void suspend() { checkAccess(); suspend0(); } /** * Resumes a suspended 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> (in the current thread). * <p> * If the thread is alive but suspended, it is resumed and is * permitted to make progress in its execution. * * @exception SecurityException if the current thread cannot modify this * thread. * @see #checkAccess * @see #suspend() * @deprecated This method exists solely for use with {@link #suspend}, * which has been deprecated because it is deadlock-prone. * For more information, see * <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. */ @Deprecated public final void resume() { checkAccess(); resume0(); }
- setPriority()、getPriority()、setName()、getName()、getThreadGroup() —— 普通的setter、getter
/** * 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); } } /** * Returns this thread's priority. * * @return this thread's priority. * @see #setPriority */ public final int getPriority() { return priority; } /** * Changes the name of this thread to be equal to the argument * <code>name</code>. * <p> * First the <code>checkAccess</code> method of this thread is called * with no arguments. This may result in throwing a * <code>SecurityException</code>. * * @param name the new name for this thread. * @exception SecurityException if the current thread cannot modify this * thread. * @see #getName * @see #checkAccess() */ 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); } } /** * Returns this thread's name. * * @return this thread's name. * @see #setName(String) */ public final String getName() { return name; } /** * Returns the thread group to which this thread belongs. * This method returns null if this thread has died * (been stopped). * * @return this thread's thread group. */ public final ThreadGroup getThreadGroup() { return group; }
- enumerate(Thread[] tarray)
/** * 复制当前线程所属线程组及其子线程组的所有活跃线程到指定的数组中,并返回复制的数量 * */ public static int enumerate(Thread tarray[]) { return currentThread().getThreadGroup().enumerate(tarray); }
- countStackFrames() —— 已废弃,不再介绍
/** * Counts the number of stack frames in this thread. The thread must * be suspended. * * @return the number of stack frames in this thread. * @exception IllegalThreadStateException if this thread is not * suspended. * @deprecated The definition of this call depends on {@link #suspend}, * which is deprecated. Further, the results of this call * were never well-defined. */ @Deprecated public native int countStackFrames();
- join()、join(long)、join(long, int)
/** * 停止当前线程,直到调用线程消亡 * 当前线程只得是Thread.currentThread()。比如: * public static void main(String[] args) { * Thread thread = new Thread(){ * ....... * } * thread.start(); * thread.join(); // 此时main线程被停止,而thread这个线程将会被调度执行,等到thread线程执行结束,main线程才会继续执行 * } */ public final void join() throws InterruptedException { join(0); } /** * 停止当前线程millis毫秒等待调用线程执行结束,millis毫秒后即使调用线程没有结束, * 当前线程也会重新进入就绪状态 */ public final synchronized void join(long millis) throws InterruptedException { long base = System.currentTimeMillis(); long now = 0; if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (millis == 0) { while (isAlive()) { wait(0); } } else { while (isAlive()) { long delay = millis - now; if (delay <= 0) { break; } wait(delay); now = System.currentTimeMillis() - base; } } } /** * join的纳秒支持模式,与sleep(long, int)实现方式一样,都不是特别精确 */ public final synchronized void join(long millis, int nanos) throws InterruptedException { if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (nanos < 0 || nanos > 999999) { throw new IllegalArgumentException( "nanosecond timeout value out of range"); } if (nanos >= 500000 || (nanos != 0 && millis == 0)) { millis++; } join(millis); }
- dumpStack()
/** * 打印线程的调用栈 */ public static void dumpStack() { new Exception("Stack trace").printStackTrace(); }
- setDaemon()、isDaemon()
/** * 设置线程为守护线程。不能再线程为活跃状态时调用 */ public final void setDaemon(boolean on) { checkAccess(); if (isAlive()) { throw new IllegalThreadStateException(); } daemon = on; } /** * Tests if this thread is a daemon thread. * * @return <code>true</code> if this thread is a daemon thread; * <code>false</code> otherwise. * @see #setDaemon(boolean) */ public final boolean isDaemon() { return daemon; }
- checkAccess()
/** * 检查当前线程是否有权修改调用线程 */ public final void checkAccess() { SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkAccess(this); } }
- getContextClassLoader()、setContextClassLoader()
/** * Returns the context ClassLoader for this Thread. The context * ClassLoader is provided by the creator of the thread for use * by code running in this thread when loading classes and resources. * If not {@linkplain #setContextClassLoader set}, the default is the * ClassLoader context of the parent Thread. The context ClassLoader of the * primordial thread is typically set to the class loader used to load the * application. * * <p>If a security manager is present, and the invoker's class loader is not * {@code null} and is not the same as or an ancestor of the context class * loader, then this method invokes the security manager's {@link * SecurityManager#checkPermission(java.security.Permission) checkPermission} * method with a {@link RuntimePermission RuntimePermission}{@code * ("getClassLoader")} permission to verify that retrieval of the context * class loader is permitted. * * @return the context ClassLoader for this Thread, or {@code null} * indicating the system class loader (or, failing that, the * bootstrap class loader) * * @throws SecurityException * if the current thread cannot get the context ClassLoader * * @since 1.2 */ @CallerSensitive public ClassLoader getContextClassLoader() { if (contextClassLoader == null) return null; SecurityManager sm = System.getSecurityManager(); if (sm != null) { ClassLoader.checkClassLoaderPermission(contextClassLoader, Reflection.getCallerClass()); } return contextClassLoader; } /** * Sets the context ClassLoader for this Thread. The context * ClassLoader can be set when a thread is created, and allows * the creator of the thread to provide the appropriate class loader, * through {@code getContextClassLoader}, to code running in the thread * when loading classes and resources. * * <p>If a security manager is present, its {@link * SecurityManager#checkPermission(java.security.Permission) checkPermission} * method is invoked with a {@link RuntimePermission RuntimePermission}{@code * ("setContextClassLoader")} permission to see if setting the context * ClassLoader is permitted. * * @param cl * the context ClassLoader for this Thread, or null indicating the * system class loader (or, failing that, the bootstrap class loader) * * @throws SecurityException * if the current thread cannot set the context ClassLoader * * @since 1.2 */ public void setContextClassLoader(ClassLoader cl) { SecurityManager sm = System.getSecurityManager(); if (sm != null) { sm.checkPermission(new RuntimePermission("setContextClassLoader")); } contextClassLoader = cl; }
- holdsLock(Object)
/** * 判断当前线程是否持有指定的对象锁 */ public static native boolean holdsLock(Object obj);
- getStackTrace()
/** * 以数组的形式获取该线程的调用栈信息,返回值的第一个元素表示最近一次的调用, * 最后一个元素表示最远一次的调用。如果该进程处于新建状态、 * 处于就绪状态但尚未被调度器调度、已经终结,则返回一个空数组 */ public StackTraceElement[] getStackTrace() { // 如果不是获取当前线程的调用栈,则需要进行权限校验,并且要调用本地方法来实现调用栈获取 if (this != Thread.currentThread()) { // check for getStackTrace permission SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkPermission( SecurityConstants.GET_STACK_TRACE_PERMISSION); } // optimization so we do not call into the vm for threads that // have not yet started or have terminated if (!isAlive()) { return EMPTY_STACK_TRACE; } StackTraceElement[][] stackTraceArray = dumpThreads(new Thread[] {this}); StackTraceElement[] stackTrace = stackTraceArray[0]; // a thread that was alive during the previous isAlive call may have // since terminated, therefore not having a stacktrace. if (stackTrace == null) { stackTrace = EMPTY_STACK_TRACE; } return stackTrace; } else { // 获取当前线程的调用栈则无需虚拟机帮助,借助异常来获取即可 // Don't need JVM help for current thread return (new Exception()).getStackTrace(); } } /** * 获取所有活跃线程的调用栈信息,以Map形式返回 */ public static Map<Thread, StackTraceElement[]> getAllStackTraces() { // check for getStackTrace permission SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkPermission( SecurityConstants.GET_STACK_TRACE_PERMISSION); security.checkPermission( SecurityConstants.MODIFY_THREADGROUP_PERMISSION); } // Get a snapshot of the list of all threads Thread[] threads = getThreads(); StackTraceElement[][] traces = dumpThreads(threads); Map<Thread, StackTraceElement[]> m = new HashMap<>(threads.length); for (int i = 0; i < threads.length; i++) { StackTraceElement[] stackTrace = traces[i]; if (stackTrace != null) { m.put(threads[i], stackTrace); } // else terminated so we don't put it in the map } return m; }
- dumpThreads()、getThreads()
private native static StackTraceElement[][] dumpThreads(Thread[] threads); private native static Thread[] getThreads();
- isCCLOverridden(Class<?>)
/** * 检查该类是否重写了有安全性限制又没有被final修饰的类,其实就是getContextClassLoader和setContextClassLoader */ private static boolean isCCLOverridden(Class<?> cl) { if (cl == Thread.class) return false; // 这个地方采用虚引用的方式进行缓存,值得借鉴学习 processQueue(Caches.subclassAuditsQueue, Caches.subclassAudits); WeakClassKey key = new WeakClassKey(cl, Caches.subclassAuditsQueue); Boolean result = Caches.subclassAudits.get(key); if (result == null) { result = Boolean.valueOf(auditSubclass(cl)); Caches.subclassAudits.putIfAbsent(key, result); } return result.booleanValue(); } /** * 清除无用的缓存 */ static void processQueue(ReferenceQueue<Class<?>> queue, ConcurrentMap<? extends WeakReference<Class<?>>, ?> map) /** * 检查 */ private static boolean auditSubclass(final Class<?> subcl) { Boolean result = AccessController.doPrivileged( new PrivilegedAction<Boolean>() { public Boolean run() { for (Class<?> cl = subcl; cl != Thread.class; cl = cl.getSuperclass()) { try { cl.getDeclaredMethod("getContextClassLoader", new Class<?>[0]); return Boolean.TRUE; } catch (NoSuchMethodException ex) { } try { Class<?>[] params = {ClassLoader.class}; cl.getDeclaredMethod("setContextClassLoader", params); return Boolean.TRUE; } catch (NoSuchMethodException ex) { } } return Boolean.FALSE; } } ); return result.booleanValue(); }
- getId()
/** * 获取线程id,tid是一个自增的计数器变量 */ public long getId() { return tid; }
- getState()
/** * A thread state. A thread can be in one of the following states: * <ul> * <li>{@link #NEW}<br> * A thread that has not yet started is in this state. * </li> * <li>{@link #RUNNABLE}<br> * A thread executing in the Java virtual machine is in this state. * </li> * <li>{@link #BLOCKED}<br> * A thread that is blocked waiting for a monitor lock * is in this state. * </li> * <li>{@link #WAITING}<br> * A thread that is waiting indefinitely for another thread to * perform a particular action is in this state. * </li> * <li>{@link #TIMED_WAITING}<br> * A thread that is waiting for another thread to perform an action * for up to a specified waiting time is in this state. * </li> * <li>{@link #TERMINATED}<br> * A thread that has exited is in this state. * </li> * </ul> * * <p> * A thread can be in only one state at a given point in time. * These states are virtual machine states which do not reflect * any operating system thread states. * * @since 1.5 * @see #getState */ public enum State { /** * Thread state for a thread which has not yet started. * 新建状态,尚未调用start */ NEW, /** * Thread state for a runnable thread. A thread in the runnable * state is executing in the Java virtual machine but it may * be waiting for other resources from the operating system * such as processor. * 运行(包括就绪状态和运行两种态) */ RUNNABLE, /** * Thread state for a thread blocked waiting for a monitor lock. * A thread in the blocked state is waiting for a monitor lock * to enter a synchronized block/method or * reenter a synchronized block/method after calling * {@link Object#wait() Object.wait}. * 阻塞状态(锁等待引起的阻塞) */ BLOCKED, /** * Thread state for a waiting thread. * A thread is in the waiting state due to calling one of the * following methods: * <ul> * <li>{@link Object#wait() Object.wait} with no timeout</li> * <li>{@link #join() Thread.join} with no timeout</li> * <li>{@link LockSupport#park() LockSupport.park}</li> * </ul> * * <p>A thread in the waiting state is waiting for another thread to * perform a particular action. * * For example, a thread that has called <tt>Object.wait()</tt> * on an object is waiting for another thread to call * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on * that object. A thread that has called <tt>Thread.join()</tt> * is waiting for a specified thread to terminate. * 等待/挂起状态(由于wait()或者park()方法调用引起的, join()方法的本质也是wait方法) */ WAITING, /** * Thread state for a waiting thread with a specified waiting time. * A thread is in the timed waiting state due to calling one of * the following methods with a specified positive waiting time: * <ul> * <li>{@link #sleep Thread.sleep}</li> * <li>{@link Object#wait(long) Object.wait} with timeout</li> * <li>{@link #join(long) Thread.join} with timeout</li> * <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li> * <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li> * </ul> * 有超时时间的限制的等待/挂起状态,与waiting的一种特叔情况 */ TIMED_WAITING, /** * Thread state for a terminated thread. * The thread has completed execution. * 终结状态 */ TERMINATED; } /** * 获取线程状态 */ public State getState() { // get current thread state return sun.misc.VM.toThreadState(threadStatus); }
- setDefaultUncaughtExceptionHandler()、getDefaultUncaughtExceptionHandler()
@FunctionalInterface public interface UncaughtExceptionHandler { /** * 当线程抛出异常时会回调该方法 */ void uncaughtException(Thread t, Throwable e); } /** * setter */ public static void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh) { SecurityManager sm = System.getSecurityManager(); if (sm != null) { sm.checkPermission( new RuntimePermission("setDefaultUncaughtExceptionHandler") ); } defaultUncaughtExceptionHandler = eh; } /** * getter */ public static UncaughtExceptionHandler getDefaultUncaughtExceptionHandler(){ return defaultUncaughtExceptionHandler; } /** * getter */ public UncaughtExceptionHandler getUncaughtExceptionHandler() { return uncaughtExceptionHandler != null ? uncaughtExceptionHandler : group; } /** * setter */ public void setUncaughtExceptionHandler(UncaughtExceptionHandler eh) { checkAccess(); uncaughtExceptionHandler = eh; }
- dispatchUncaughtException()
/** * 发生异常时,虚拟机调用该方法回调对应的异常处理器 */ private void dispatchUncaughtException(Throwable e) { getUncaughtExceptionHandler().uncaughtException(this, e); }
- 一些本地方法实现
rivate native void setPriority0(int newPriority); private native void stop0(Object o); private native void suspend0(); private native void resume0(); private native void interrupt0(); private native void setNativeName(String name)