JDK源碼(二十):Thread

進程

進程是資源(CPU、內存等)分配的基本單位,它是程序執行時的一個實例。程序運行時系統就會創建一個進程,併爲它分配資源,然後把該進程放入進程就緒隊列,進程調度器選中它的時候就會爲它分配CPU時間,程序開始真正運行。

線程

線程是程序執行時的最小單位,它是進程的一個執行流,是CPU調度和分派的基本單位,一個進程可以由很多個線程組成,線程間共享進程的所有資源,每個線程有自己的堆棧和局部變量。線程由CPU獨立調度執行,在多CPU環境下就允許多個線程同時運行。同樣多線程也可以實現併發操作,每個請求分配一個線程來處理。

線程是程序中的執行線程。Java虛擬機允許應用程序同時運行多個執行線程。每個線程都有優先級。優先級較高的線程優先於優先級較低的線程執行。每個線程也可以標記爲守護進程,也可以不標記爲守護進程。當在某個線程中運行的代碼創建一個新的<code>線程</code>對象時,新線程的優先級最初設置爲等於創建線程的優先級,並且僅當創建線程是守護進程時纔是守護進程線程。每個線程都有一個用於標識的名稱。多個線程可能具有相同的名稱。如果在創建線程時未指定名稱,則會爲其生成新名稱。

類名

public class Thread implements Runnable 

變量

    /* 線程名稱 */
    private volatile String name;
	/* 線程優先級 */
    private int            priority;
    private Thread         threadQ;//類中沒有使用到
    private long           eetop;//類中沒有使用到
    /* 是否單步執行此線程 */
    private boolean     single_step;
    /* 線程是否爲守護進程線程。 */
    private boolean     daemon = false;
    /* JVM state */
    private boolean     stillborn = false;
    /* 將要運行的內容 */
    private Runnable target;
    /* 線程組 */
    private ThreadGroup group;
    /* 此線程的上下文類加載器*/
    private ClassLoader contextClassLoader;
    /* 此線程繼承的AccessControlContext*/
    private AccessControlContext inheritedAccessControlContext;
    /* 用於自動編號匿名線程. */
    private static int threadInitNumber;
    private static synchronized int nextThreadNum() {
        return threadInitNumber++;
    }
    /* 與此線程相關的ThreadLocal值。此映射由ThreadLocal類維護。*/
    ThreadLocal.ThreadLocalMap threadLocals = null;
    /*
     * 與此線程相關的可繼承線程本地值。此映射由InheritableThreadLocal類維護。
     */
    ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
    /*
     * 此線程請求的堆棧大小,如果創建者未指定堆棧大小,則爲0。一些VM會忽略它
     */
    private long stackSize;
    /*
     * 在本機線程終止後持續存在的JVM私有狀態
     */
    private long nativeParkEventPointer;

線程創建

1.繼承Thread類

public class ThreadDemo extends Thread {
    @Override
    public void run() {
        System.out.println("Thread");
    }
}

2.實現Runnable接口

public class ThreadDemo implements Runnable {
    @Override
    public void run() {
        System.out.println("Runnable");
    }
}

3.使用Callable和Future

public class ThreadDemo implements Callable<String> {
    @Override
    public String call() throws Exception {
        System.out.println(Thread.currentThread().getName());
        return Thread.currentThread().getName();
    }
    public static void main(String[] args) throws Exception {
        Callable<String> callable = new ThreadDemo();
        FutureTask<String> ft = new FutureTask<>(callable);
        new Thread(ft,"threadName").start();
        System.out.println(ft.get());
    }
}

線程狀態

 

start()

    /**
     * 使此線程開始執行;Java虛擬機調用此線程的run方法。
     */
    public synchronized void start() {
        /**
         *對於VM創建/設置的主方法線程或“系統”組線程,
		 *不調用此方法。將來添加到此方法的任何新功能可能也必須添加到VM中。
         */
        if (threadStatus != 0)
            throw new IllegalThreadStateException();

        /* 通知group此線程即將啓動,
		 * 以便可以將其添加到組的線程列表中,並且可以減少組的未啓動計數 */
        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()

    /**
     * 如果此線程是使用單獨的Runnable run對象構造的,
	 * 則調用該Runnable對象的run方法;否則,此方法不執行任何操作和返回。
     * 子類需要重寫這個方法
     */
    @Override
    public void run() {
        if (target != null) {
            target.run();
        }
    }

真正啓動線程的是start()方法而不是run(),run()和普通的成員方法一樣,可以重複使用,但不能啓動一個新線程。

sleep()和wait()的區別

sleep爲線程的方法,而wait爲Object的方法,他們的功能相似,最大本質的區別是:sleep不釋放鎖,wait釋放鎖。

sleep(milliseconds)可以用時間指定來使他自動醒過來,如果時間不到你只能調用interreput()來終止線程;wait()可以用notify()/notifyAll()直接喚起。

public class ThreadDemo extends Thread {
    int number = 10;
    public synchronized void add(){
        number = number+1;
        System.out.println(number);
    }
    public synchronized void multiply() throws InterruptedException {
        //Thread.sleep(1000);
        this.wait(0);
        number = number*100;
    }
    @Override
    public void run() {
        add();
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadDemo demo = new ThreadDemo();
        demo.start();
        demo.interrupt();
        Thread.sleep(1000);
        System.out.println(demo.number);
    }
}

當調用sleep方法輸出 1001,調用wait方法輸出1100。

join

    /**
     * 等待此線程結束.
     */
    public final void join() throws InterruptedException {
        join(0);
    }
    /**
     * 最多等待millis毫秒,0意味着永遠等待。
     */
    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()方法,使調用此方法的線程wait(),直到調用此方法的線程對象所在的線程執行完畢後被喚醒。

interrupt

    /**
     * 中斷此線程.
     */
    public void interrupt() {
        if (this != Thread.currentThread())
            checkAccess();

        synchronized (blockerLock) {
            Interruptible b = blocker;
            if (b != null) {
                interrupt0(); //只是爲了設置中斷標誌
                b.interrupt(this);
                return;
            }
        }
        interrupt0();
    }

當對一個線程,調用 interrupt() 時,
① 如果線程處於被阻塞狀態(例如處於sleep, wait, join 等狀態),那麼線程將立即退出被阻塞狀態,並拋出一個InterruptedException異常。僅此而已。
② 如果線程處於正常活動狀態,那麼會將該線程的中斷標誌設置爲 true,僅此而已。被設置中斷標誌的線程將繼續正常運行,不受影響。

public class Thread implements Runnable {
    /* Make sure registerNatives is the first thing <clinit> does. */
    private static native void registerNatives();
    static {
        registerNatives();
    }
    /* 線程名稱 */
    private volatile String name;
	/* 線程優先級 */
    private int            priority;
    private Thread         threadQ;//類中沒有使用到
    private long           eetop;//類中沒有使用到
    /* 是否單步執行此線程 */
    private boolean     single_step;
    /* 線程是否爲守護進程線程。 */
    private boolean     daemon = false;
    /* JVM state */
    private boolean     stillborn = false;
    /* 將要運行的內容 */
    private Runnable target;
    /* 線程組 */
    private ThreadGroup group;
    /* 此線程的上下文類加載器*/
    private ClassLoader contextClassLoader;
    /* 此線程繼承的AccessControlContext*/
    private AccessControlContext inheritedAccessControlContext;
    /* 用於自動編號匿名線程. */
    private static int threadInitNumber;
    private static synchronized int nextThreadNum() {
        return threadInitNumber++;
    }
    /* 與此線程相關的ThreadLocal值。此映射由ThreadLocal類維護。*/
    ThreadLocal.ThreadLocalMap threadLocals = null;
    /*
     * 與此線程相關的可繼承線程本地值。此映射由InheritableThreadLocal類維護。
     */
    ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
    /*
     * 此線程請求的堆棧大小,如果創建者未指定堆棧大小,則爲0。一些VM會忽略它
     */
    private long stackSize;
    /*
     * 在本機線程終止後持續存在的JVM私有狀態
     */
    private long nativeParkEventPointer;
    /*
     * Thread ID
     */
    private long tid;
    /* 用於生成線程ID */
    private static long threadSeqNumber;
    /* Java線程狀態,已初始化表示線程“尚未啓動”
     */
    private volatile int threadStatus = 0;


    private static synchronized long nextThreadID() {
        return ++threadSeqNumber;
    }

    /**
     * 提供給當前調用java.util.concurrent.locks.LockSupport.park的參數。
	 * 由(私有)java.util.concurrent.locks.LockSupport.setBlocker設置
	 * 使用java.util.concurrent.locks.LockSupport.getBlocker訪問
     */
    volatile Object parkBlocker;

    /* 此線程在可中斷I/O操作中被阻塞的對象(如果有)。
	 * 設置此線程的中斷狀態後,應調用阻止程序的中斷方法。
     */
    private volatile Interruptible blocker;
    private final Object blockerLock = new Object();

    /* 設置blocker字段;從java.nio代碼通過sun.misc.SharedSecrets調用
     */
    void blockedOn(Interruptible b) {
        synchronized (blockerLock) {
            blocker = b;
        }
    }

    /**
     * 線程可以具有的最小優先級.
     */
    public final static int MIN_PRIORITY = 1;

   /**
     * 分配給線程的默認優先級.
     */
    public final static int NORM_PRIORITY = 5;

    /**
     * 線程可以擁有的最大優先級。.
     */
    public final static int MAX_PRIORITY = 10;

    /**
     * 返回對當前正在執行的線程對象的引用。
     */
    public static native Thread currentThread();

    /**
     * 向調度程序提示當前線程願意放棄當前對處理器的使用。
     * Yield是一個啓發式的嘗試,旨在改進線程之間的相對進程,
	 * 否則會過度使用CPU。它的使用應該與詳細的概要分析和基準測試相結合,
	 * 以確保它實際具有預期的效果。
     * 使用這種方法是不合適的。它可能對調試或測試有用。
     */
    public static native void yield();

    /**
     * 根據系統計時器和調度程序的精度和準確性,
	 * 使當前執行的線程睡眠(暫時停止執行)指定的毫秒數。
	 * 線程不會失去任何監視器的所有權.
     */
    public static native void sleep(long millis) throws InterruptedException;
	
    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");
        }

        if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
            millis++;
        }

        sleep(millis);
    }

    /**
     * 使用當前AccessControlContext初始化線程.
     */
    private void init(ThreadGroup g, Runnable target, String name,
                      long stackSize) {
        init(g, target, name, stackSize, null, 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) {
                g = security.getThreadGroup();
            }
            if (g == null) {
                g = parent.getThreadGroup();
            }
        }
        /* 無論是否顯式傳入threadgroup,都可以進行checkAccess. */
        g.checkAccess();
        /*
         * 檢查權限
         */
        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();
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }

    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);
    }


    public Thread(ThreadGroup group, Runnable target, String name,
                  long stackSize) {
        init(group, target, name, stackSize);
    }

    /**
     * 使此線程開始執行;Java虛擬機調用此線程的run方法。
     */
    public synchronized void start() {
        /**
         *對於VM創建/設置的主方法線程或“系統”組線程,
		 *不調用此方法。將來添加到此方法的任何新功能可能也必須添加到VM中。
         */
        if (threadStatus != 0)
            throw new IllegalThreadStateException();

        /* 通知group此線程即將啓動,
		 * 以便可以將其添加到組的線程列表中,並且可以減少組的未啓動計數 */
        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();

    /**
     * 如果此線程是使用單獨的Runnable run對象構造的,
	 * 則調用該Runnable對象的run方法;否則,此方法不執行任何操作和返回。
     * 子類需要重寫這個方法
     */
    @Override
    public void run() {
        if (target != null) {
            target.run();
        }
    }

    /**
     * 這個方法被系統調用,以便在實際退出之前給線程一個清理的機會。
     */
    private void exit() {
        if (group != null) {
            group.threadTerminated(this);
            group = null;
        }
        /* Aggressively null out all reference fields: see bug 4006245 */
        target = null;
        /* 加快釋放這些資源 */
        threadLocals = null;
        inheritableThreadLocals = null;
        inheritedAccessControlContext = null;
        blocker = null;
        uncaughtExceptionHandler = null;
    }

    /**
     * 中斷此線程.
     */
    public void interrupt() {
        if (this != Thread.currentThread())
            checkAccess();

        synchronized (blockerLock) {
            Interruptible b = blocker;
            if (b != null) {
                interrupt0(); //只是爲了設置中斷標誌
                b.interrupt(this);
                return;
            }
        }
        interrupt0();
    }

    /**
     * 當前線程是否已中斷. 
     */
    public static boolean interrupted() {
        return currentThread().isInterrupted(true);
    }

    /**
     * 此線程是否已中斷. 
     */
    public boolean isInterrupted() {
        return isInterrupted(false);
    }

    /**
     * 某個線程是否被中斷。中斷狀態是否基於傳遞的ClearInterrupted值重置。
     */
    private native boolean isInterrupted(boolean ClearInterrupted);

    /**
     * 此線程是否處於活動狀態。如果線程已啓動但尚未死亡,則它是活動的
     */
    public final native boolean isAlive();

    /**
     * 更改此線程的優先級.
     */
    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);
        }
    }

    /**
     * 返回當前線程的優先級
     */
    public final int getPriority() {
        return priority;
    }

    /**
     * 修改線程名稱
     */
    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;
    }

    /**
     * 返回此線程所屬的線程組。
	 * 如果此線程已終止(已停止),則此方法返回null
     */
    public final ThreadGroup getThreadGroup() {
        return group;
    }

    /**
     * 返回當前線程的及其子組中活動線程數的估計值。
	 * 遞歸地迭代當前線程的線程組中的所有子組。
     */
    public static int activeCount() {
        return currentThread().getThreadGroup().activeCount();
    }

    /**
     *將當前線程的線程組及其子組中的每個活動線程複製到指定數組中
     */
    public static int enumerate(Thread tarray[]) {
        return currentThread().getThreadGroup().enumerate(tarray);
    }

   /**
     * 等待此線程結束.
     */
    public final void join() throws InterruptedException {
        join(0);
    }
    /**
     * 最多等待millis毫秒,0意味着永遠等待。
     */
    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;
            }
        }
    }

    /**
     * 最多等待millis毫秒加上nanos納秒,使此線程死亡。
     */
    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);
    }

    /**
     * 將當前線程的堆棧跟蹤打印到標準錯誤流。此方法僅用於調試。
     */
    public static void dumpStack() {
        new Exception("Stack trace").printStackTrace();
    }

    /**
     * 將此線程標記爲守護線程或用戶線程。
	 * 當運行的唯一線程都是守護進程線程時,java虛擬機退出。
     * 必須在啓動線程之前調用此方法
     */
    public final void setDaemon(boolean on) {
        checkAccess();
        if (isAlive()) {
            throw new IllegalThreadStateException();
        }
        daemon = on;
    }

    /**
     * 此線程是否守護進程線程。
     */
    public final boolean isDaemon() {
        return daemon;
    }

    /**
     * 確定當前運行的線程是否具有修改此線程的權限
     */
    public final void checkAccess() {
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkAccess(this);
        }
    }

    /**
     * 返回此線程的字符串表示形式,包括線程的名稱、優先級和線程組。
     */
    public String toString() {
        ThreadGroup group = getThreadGroup();
        if (group != null) {
            return "Thread[" + getName() + "," + getPriority() + "," +
                           group.getName() + "]";
        } else {
            return "Thread[" + getName() + "," + getPriority() + "," +
                            "" + "]";
        }
    }

    /**
     * 返回此線程的上下文類加載器
     */
    @CallerSensitive
    public ClassLoader getContextClassLoader() {
        if (contextClassLoader == null)
            return null;
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            ClassLoader.checkClassLoaderPermission(contextClassLoader,
                                                   Reflection.getCallerClass());
        }
        return contextClassLoader;
    }

    /**
     * 設置此線程的上下文類加載器. 
     */
    public void setContextClassLoader(ClassLoader cl) {
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            sm.checkPermission(new RuntimePermission("setContextClassLoader"));
        }
        contextClassLoader = cl;
    }

    /**
     * 當前線程持有指定對象上的監視器鎖時,返回true。
     */
    public static native boolean holdsLock(Object obj);

    private static final StackTraceElement[] EMPTY_STACK_TRACE
        = new StackTraceElement[0];

    /**
     * 返回表示此線程堆棧轉儲的堆棧跟蹤元素數組。
     */
    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();
        }
    }

    /**
     * 返回所有活動線程的堆棧映射。
     */
    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;
    }


    private static final RuntimePermission SUBCLASS_IMPLEMENTATION_PERMISSION =
                    new RuntimePermission("enableContextClassLoaderOverride");

    /** cache of subclass security audit results */
    /* Replace with ConcurrentReferenceHashMap when/if it appears in a future
     * release */
    private static class Caches {
        /** cache of subclass security audit results */
        static final ConcurrentMap<WeakClassKey,Boolean> subclassAudits =
            new ConcurrentHashMap<>();

        /** queue for WeakReferences to audited subclasses */
        static final ReferenceQueue<Class<?>> subclassAuditsQueue =
            new ReferenceQueue<>();
    }

    /**
     * Verifies that this (possibly subclass) instance can be constructed
     * without violating security constraints: the subclass must not override
     * security-sensitive non-final methods, or else the
     * "enableContextClassLoaderOverride" RuntimePermission is checked.
     */
    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();
    }

    /**
     * Performs reflective checks on given subclass to verify that it doesn't
     * override security-sensitive non-final methods.  Returns true if the
     * subclass overrides any of the methods, false otherwise.
     */
    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();
    }

    private native static StackTraceElement[][] dumpThreads(Thread[] threads);
    private native static Thread[] getThreads();

    /**
     * 返回此線程的標識符.
     */
    public long getId() {
        return tid;
    }

    /**
     * 線程狀態.
     */
    public enum State {
        /**
         * 尚未啓動的線程的線程狀態.
         */
        NEW,

        /**
         * 可運行線程的線程狀態.
         */
        RUNNABLE,

        /**
         * 阻塞狀態
         */
        BLOCKED,

        /**
         * 等待狀態.
         */
        WAITING,

        /**
         * 具有指定等待時間的等待線程的線程狀態。
         */
        TIMED_WAITING,

        /**
         *終止狀態
         */
        TERMINATED;
    }

    /**
     * 返回線程狀態
     */
    public State getState() {
        // get current thread state
        return sun.misc.VM.toThreadState(threadStatus);
    }

    // Added in JSR-166

    /**
     * 當線程因未捕獲異常而突然終止時調用的處理程序接口.
     */
    @FunctionalInterface
    public interface UncaughtExceptionHandler {
        /**
         * 當給定線程由於給定的未捕獲異常而終止時調用的。
         */
        void uncaughtException(Thread t, Throwable e);
    }

    // null unless explicitly set
    private volatile UncaughtExceptionHandler uncaughtExceptionHandler;

    // null unless explicitly set
    private static volatile UncaughtExceptionHandler defaultUncaughtExceptionHandler;

    /**
     * 設置當線程由於未捕獲異常而突然終止時調用的默認處理程序,
	 * 並且尚未爲該線程定義其他處理程序。
     */
    public static void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh) {
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            sm.checkPermission(
                new RuntimePermission("setDefaultUncaughtExceptionHandler")
                    );
        }

         defaultUncaughtExceptionHandler = eh;
     }

    public static UncaughtExceptionHandler getDefaultUncaughtExceptionHandler(){
        return defaultUncaughtExceptionHandler;
    }

    public UncaughtExceptionHandler getUncaughtExceptionHandler() {
        return uncaughtExceptionHandler != null ?
            uncaughtExceptionHandler : group;
    }

    public void setUncaughtExceptionHandler(UncaughtExceptionHandler eh) {
        checkAccess();
        uncaughtExceptionHandler = eh;
    }

    private void dispatchUncaughtException(Throwable e) {
        getUncaughtExceptionHandler().uncaughtException(this, e);
    }

    static void processQueue(ReferenceQueue<Class<?>> queue,
                             ConcurrentMap<? extends
                             WeakReference<Class<?>>, ?> map)
    {
        Reference<? extends Class<?>> ref;
        while((ref = queue.poll()) != null) {
            map.remove(ref);
        }
    }

    /**
     *  Weak key for Class objects.
     **/
    static class WeakClassKey extends WeakReference<Class<?>> {

        private final int hash;

        WeakClassKey(Class<?> cl, ReferenceQueue<Class<?>> refQueue) {
            super(cl, refQueue);
            hash = System.identityHashCode(cl);
        }

        @Override
        public int hashCode() {
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == this)
                return true;

            if (obj instanceof WeakClassKey) {
                Object referent = get();
                return (referent != null) &&
                       (referent == ((WeakClassKey) obj).get());
            } else {
                return false;
            }
        }
    }

    /** The current seed for a ThreadLocalRandom */
    @sun.misc.Contended("tlr")
    long threadLocalRandomSeed;

    /** Probe hash value; nonzero if threadLocalRandomSeed initialized */
    @sun.misc.Contended("tlr")
    int threadLocalRandomProbe;

    /** Secondary seed isolated from public ThreadLocalRandom sequence */
    @sun.misc.Contended("tlr")
    int threadLocalRandomSecondarySeed;

    /* Some private helper methods */
    private 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);
}

更多精彩內容請關注微信公衆號:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章