Java Thread源码分析(JDK8)

线程简介

操作系统在运行一个程序时,会为其创建一个进程,在一个进程里可以创建多个线程,线程作为操作系统调度的最小单元,一个线程包含:各自的计数器、堆栈以及局部变量(能够访问共享的内存)。

为什么使用多线程
  • 现在的处理器都是多核心的,一个线程虽然在一个时刻只能运行在一个处理器的核心,但是可以利用多线程技术是资源发挥最大的作用。
  • 多线程执行单线程的顺序任务,可提高效率,减少系统的响应时间。
  • Java 提供了现成的API,不需要知道多线程是怎么实现的。
线程的状态

Java线程在运行的生命周期中可能会有6中状态:

  • NEW :初始状态,线程被创建,但是还没有调用start()方法。
  • RUNNABLE :运行装填,就是创建的线程执行了start()方法,这个状态下又分为两种状态:就绪(就是线程执行了start方法,但是没有获得CPU的执行)和运行(CPU正在执行该线程),这两种统称为“运行中”状态。
  • BLOCKED : 阻塞状态,表示线程阻塞于锁。
  • WAITING :等待状态,表示线程进入等待状态,进入该状态表示当前线程需要等待其他线程做出一定的特定动作(通知或者中段)。
  • TIME_WAITING:等待超时状态,该状态和WAITING的区别就是它是可以在指定的时间自行返回的。
  • TERMINATE:终止状态,表示当前线程已经执行完毕。

Java线程各个状态变迁如下图:
在这里插入图片描述

线程的优先级
  • 操作系统基本均采用时分的形式调度运行的线程,处理器根据时间片算法在线程之间进行高速切换,在宏观上给人以线程同时执行的感觉。
  • 操作系统会分出一个个时间片,线程会分配到若干时间片,当线程的时间片用完了就会发生线程的调度,该线程将等待下次的时间片分配。
  • 线程的优先级就是决定线程需要多或者少分配一些处理器资源的属性,Java线程中,通过一个整型成员变量priority来控制线程的优先级,范围为1~10,默认为5,在线程创建的时候通过setPriority()方法可以修改优先级,优先级高的线程分配的时间片的数量要多余优先级低的线程,但是不是绝对,很多操作系统以及JVM版本并不认可线程的优先级的说法。
Daemon线程(守护线程)
  • Daemon线程是一个支持型线程,它主要被用于程序中的后台调度以及支持性工作。
  • 线程设置Daemon属性需要在启动线程之前设置。
  • Daemon线程咋Java虚拟机退出时,Daemon线程中的finally块并不一定会执行,所以不能依靠finally块来关闭资源等操作。
附:Thread源码
    package java.lang;
    /**
     * 操作系统在运行一个程序时,会为其创建一个进程,一个进程中可以创建多个线程, 这些线程都拥有各自的计数器,堆栈和局部变量等属性,
     * 并且可以访问共享的内存变量, 处理器根据时间片算法在线程之间进行高速切换,在宏观上给人以线程同时执行的感觉.
     *
     * 每个线程都有相应的优先级,从1到10,默认优先级为5,高优先级的线程获得CPU时间片优势大于低优先级的, 但是不是绝对,很多操作系统以及JVM版本并不认可线程的优先级的说法
     *
     * 每个线程都可以被设置为守护线程(Daemon),设置时在线程启动前设置,调用Thread.setDaemon(true),守护线程
     * 主要用于程序中后台调度以及支持性工作.当JVM中不存在Daemon线程时,JVM会退出,所以Daemon线程中的finally中的代码 不一定会执行
     *
     * 创建线程一般有两种方法: 集成Thread类,实现Runnable接口 实际上Thread也是实现Runable接口
     */
    public class Thread implements Runnable {
        /** static修饰,确保本地注册创建线程时执行,native修饰,调用其他语言的方法 */
        private static native void registerNatives();
	    static {
	        registerNatives();
	    }
	
  	  	private volatile char name[]; 	// 线程名,可以修改且对线程可见
    	private int priority;	// 线程优先级,1到10,默认为5
  	 	private Thread threadQ;
    	private long eetop;
    	
    	  /* 是否是单步执行*/
   		private boolean single_step;
   		
		  /* 该线程是否是守护线程 */
	    private boolean daemon = false; 
	
	    /* JVM 状态 */
	    private boolean stillborn = false;
	
	    /* 要被执行的run方法 */
	    private Runnable target;
	
	    /* 线程所属线程组 */
	    private ThreadGroup group;
	
	    /* 线程的类加载器 */
	    private ClassLoader contextClassLoader;
	
	    /* 该线程继承的访问控制上下文 */
	    private AccessControlContext inheritedAccessControlContext;
	
	    /* 用于自动编号的匿名线程 ,默认线程编号*/
	    private static int threadInitNumber;
	
	    private static synchronized int nextThreadNum() {
	        return threadInitNumber++;
	    }
	
	    /* 该线程的本地变量值,此map由ThreadLocal类进行维护,是一个维护线程本地变量的hashmap,hashmap的key为弱引用,可以增长对象的存活时间*/
	    ThreadLocal.ThreadLocalMap threadLocals = null;
	
	    /*该线程继承所得到本地变量值,此map由inheritableThreadLocals类维护*/
	    ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
	
	    /*栈的深度,默认为0,此数字如何被使用完全取决于虚拟机自己;也有一些虚拟机会忽略此变量值.*/
	    private long stackSize;

	    /*本地线程终止后,JVM私有的一个状态值*/
	    private long nativeParkEventPointer;

	    /* 线程id*/
	    private long tid;
	
	    /* 用于生成线程Id */
	    private static long threadSeqNumber;
	
	    /* 为工具提供的线程状态值,初始化值标识当前线程还未运行*/
	    private volatile int threadStatus = 0;

	    /* 获取下一个线程id*/
	    private static synchronized long nextThreadID() {
	        return ++threadSeqNumber;
	    }

	    /* 线程共享变量d*/
	    volatile Object parkBlocker;

	    /* 阻塞变量,如果线程的中断状态被设置,则应该调用此线程阻塞对象的中断方法*/
	    private volatile Interruptible blocker;
	    private final Object blockerLock = new Object();

	    /* 设置block的值,*/
	    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;
	
	    /* 返回当前正在执行线程对象的应用,这是一个native修饰的方法*/
	    public static native Thread currentThread();
	
	    /**
	     * 提示线程调度器当前线程放弃CPU的使用,当然线程调度器可以忽略,
	     * 让出CPU是一种启发式尝试,已改善线程之间的相对发展,否者将会过度利用CPU或者是其他线程获得不了CPU执行权限
	     */
	    public static native void yield();

	    /* 睡眠millis毫秒数, 此方法的调用不会引起当前线程放弃任何监听器的所有权*/
	    public static native void sleep(long millis) throws InterruptedException;

	    /* 睡眠millis毫秒数,nanos纳秒数,,此方法的调用不会引起当前线程放弃任何监听器的所有权*/
	    public static void sleep(long millis, int nanos) throws InterruptedException {
	        // 毫秒值小于0,不合法
	        if (millis < 0) {
	            throw new IllegalArgumentException("timeout value is negative");
	        }
	        // 纳秒小于0 或者大于等于100万(1秒),纳秒不合法
	        if (nanos < 0 || nanos > 999999) {
	            throw new IllegalArgumentException("nanosecond timeout value out of range");
	        }
	        // 纳秒大于等于50万,或者有纳秒数,毫秒数为0,毫秒数加1
	        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);
	    }

	    /*初始化一个线程,私有方法,被各构造方法调用*/
	    private void init(ThreadGroup g, Runnable target, String name, long stackSize, AccessControlContext acc) {
	        // 不允许,没有名字的线程出现
	        if (name == null) {
	            throw new NullPointerException("name cannot be null");
	        }
	        // 赋值线程名字
	        this.name = name.toCharArray();
	        // 获取当正在执行的线程,用该线程来创建新的线程
	        Thread parent = currentThread();
	        // 获得系统的安全管理器
	        SecurityManager security = System.getSecurityManager();
	        if (g == null) {
	            // 线程的组为空 
	            if (security != null) {
	                // 线程的组为空,从系统安全管理器中获得线程组
	                g = security.getThreadGroup();
	            }
	
	            //系统安全管理器为null,则使用当前执行线程的组 
	            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 (parent.inheritableThreadLocals != null)
	            this.inheritableThreadLocals = ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
	        //设置栈的深度
	        this.stackSize = stackSize;
	        //设置线程id
	        tid = nextThreadID();
	    }

	    /*重写Object的克隆方法,但是线程不允许克隆.*/
	    @Override
	    protected Object clone() throws CloneNotSupportedException {
	        throw new CloneNotSupportedException();
	    }

	    /*无参构造,创建一个线程对象,线程组为安全管理器或者父线程的线程组,没有执行方法,名字默认,栈深度为0*/
	    public Thread() {
	        init(null, null, "Thread-" + nextThreadNum(), 0);
	    }

	    /*利用Runnable创建线程,有执行方法,其余均是默认状态,Runnable中有run方法*/
	    public Thread(Runnable target) {
	        init(null, target, "Thread-" + nextThreadNum(), 0);
	    }

	    /*利用Runnable创建线程,传入继承访问控制上下文*/
	    Thread(Runnable target, AccessControlContext acc) {
	        init(null, target, "Thread-" + nextThreadNum(), 0, acc);
	    }

	    /* 传入线程组以及Rannable构造*/
	    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);
	    }
	
	     /* 传入Runnable对象以及线程名构造*/
	    public Thread(Runnable target, String name) {
	        init(null, target, name, 0);
	    }

	    /* 传入线程组,Runnable对象,线程名构造*/
	    public Thread(ThreadGroup group, Runnable target, String name) {
	        init(group, target, name, 0);
	    }
	
	    /* 传入线程组,Runnable对象,线程名构,栈的深度*/
	    public Thread(ThreadGroup group, Runnable target, String name, long stackSize) {
	        init(group, target, name, stackSize);
	    }

		/** 
		* 线程启动方法
		* 此方法的调用会引起当前线程的执行;JVM会调用此线程的run()方法,
		* 并不会被主要方法线程or由虚拟机创建的系统组线程所调用
		* 任何向此方法添加的新功能方法在未来都会被添加到虚拟机中
		* 结果就是两个线程可以并发执行:当前线程(从调用的start方法返回)和另一个线程(它在执行run方法). 
		* 一个线程可以被调用多次.
		* 尤其注意:一个线程执行完成后可能并不会再被重新执行.
		*/
	    public synchronized void start() {
	        //0状态值代表了NEW的状态.
	        if (threadStatus != 0)
	            // 不是新创建的状态抛出异常
	            throw new IllegalThreadStateException();
	
	        // 向线程组里添加此线程,注意:线程创建的时候只是将线程组中的容器容量增加1
	        group.add(this);
	
	        boolean started = false;
	        try {
	            // 使线程进入可运行状态(runnable)
	            start0();
	            started = true;
	        } finally {
	            try {
	                if (!started) {
	                    // 启动失败,再线程组中删除改线程
	                    group.threadStartFailed(this);
	                }
	            } catch (Throwable ignore) {
	
	            }
	        }
	    }
	    
		/* 本地方法,使线程进入可运行状态(就绪)*/
	    private native void start0();

	    /*线程获得CPU执行权限后执行的方法,实际上是调用的Runnable中的run()*/
	    @Override
	    public void run() {
	        if (target != null) {
	            target.run();
	        }
	    }

	    /*这是在run方法执行结束后,此方法由系统调用,用于在一个线程退出前做一些扫尾工作.*/
	    private void exit() {
	        if (group != null) {
	            // 加锁执行,删除该线程
	            group.threadTerminated(this);
	            group = null;
	        }
	         // GC 回收,全部置为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;
	    }

	    /*停止方法,已过期,有风险,不建议使用*/
	    @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());
	    }
	
	    /*停止方法,已过期,有风险,不建议使用*/
	    @Deprecated
	    public final synchronized void stop(Throwable obj) {
	        throw new UnsupportedOperationException();
	    }
	
	     /**
	     *	中断当前线程, 一般来说sleep(),join(),wait()在检查到线程的中断状态的时候,
	     *	会抛出中断异常InteruptedExeption,以及清除线程的中断状态
	     */
	    public void interrupt() {
	        if (this != Thread.currentThread())
	        	//校验当前运行线程是否有权限修改本线程相关属性
	            checkAccess();
	        // 设置阻塞锁
	        synchronized (blockerLock) {
	            Interruptible b = blocker;
	            if (b != null) {
	                interrupt0();   // Just to set the interrupt flag
	                b.interrupt(this);
	                return;
	            }
	        }
	        interrupt0();
	    }
	
	    /**
	     * 判断当前线程是否是中断状态,线程的中断状态会被此方法清除,
	     * 也就是如果此方法两次调用都成功,那么在第二次调用的返回结果为false 
	     * 除非在第一次调用完后和第二次调用前,当前线程被再次中断
	     * 因为在中断状态被调用线程并处于alive状态而忽略线程中断的情况会由于此方法的调用而受影响
	     */
	    public static boolean interrupted() {
	        return currentThread().isInterrupted(true);
	    }

	    /*判断当前线程是够是中断状态,线程的中断状态不会被此方法清除*/
	    public boolean isInterrupted() {
	        return isInterrupted(false);
	    }

	     /*本地方法,测试线程是否被中断. 中断状态会被重置or并不依赖于之前的中断清除的值.*/
	    private native boolean isInterrupted(boolean ClearInterrupted);
	
	     /*过期无用方法*/
	    @Deprecated
	    public void destroy() {
	        throw new NoSuchMethodError();
	    }

	    /*本地方法, 测试当前线程是否处于存活状态,一个线程在死亡状态前都是存活状态*/
	    public final native boolean isAlive();

	    /* 过期方法*/
	    @Deprecated
	    public final void suspend() {
	        checkAccess();
	        suspend0();
	    }
	
	      /* 过期方法*/
	    @Deprecated
	    public final void resume() {
	        checkAccess();
	        resume0();
	    }

		 /*  设置当前线程的优先级,设置的优先级不能大于线程组的最大线程优先级*/
	    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();
	        this.name = name.toCharArray();
	        if (threadStatus != 0) {
	            setNativeName(name);
	        }
	    }

	    /*   获取线程的名字*/
	    public final String getName() {
	        return new String(name, true);
	    }

	    /*  获取线程的线程组*/
	    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);
	    }

	     /*  过期方法*/
	    @Deprecated
	    public native int countStackFrames();

	     /*  最多等待参数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");
	        }
	        // 等待时长为0,继续等待
	        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纳秒时长,当前线程就会死亡,参数为0时则需要继续等待*/
	    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 final void join() throws InterruptedException {
	        join(0);
	    }

	     /*  此方法只用于debug*/
	    public static void dumpStack() {
	        new Exception("Stack trace").printStackTrace();
	    }

	     /*   将当前线程设定为守护线程or用户线程. 此方法在start前被调用.如果值为true,则此线程被设定为守护线程.*/
	    public final void setDaemon(boolean on) {
	        checkAccess();
	        if (isAlive()) {
	            throw new IllegalThreadStateException();
	        }
	        daemon = on;
	    }

	    /*  判断线程是否为守护线程*/ 
	    public final boolean isDaemon() {
	        return daemon;
	    }
	
	    /**
	     * 确定当前运行的线程是否有权利更改此线程相关属性. 如果有安全管理器,则会将当前线程作为参数传入checkAccess()方法
	     *  这可能会导致SecurityException异常的抛出
	     */
	    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];
	
	    /**
	     * 返回表示该线程堆栈转储的堆栈跟踪元素数组。 如果线程还没有start,or虽然start了,但是并没有被CPU调用过,or线程以及终止了,则返回数组长度为0.
	     * 如果返回数组长度非0,则数组中第一个元素(索引为0)代表了栈的顶部,就是所有调用方法中距离现在时间最近的那个.
	     * 数组的最后一个元素代表了栈的底部,这是所有调用方法中距离现在时间最远的那个.
	     * 如果存在安全管理器,且这一线程又不是当前线程,则安全管理器的checkPermission()方法会被调用以查看
	     * 一些虚拟机在某些情况下,可能会在栈跟踪时遗漏至少一个以上的栈.在极端情况下,虚拟机没有任何栈跟踪信息所以返回数组长度为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();
	        }
	    }
	
	    /**
	     * 返回一个用于所有存活线程的栈跟踪信息的map. map的key是每个线程;value是对应线程的栈跟踪元素的一个数组.
	     * 当此方法被调用时,可能有些线程正在执行.每一个线程的栈跟踪信息都代表了线程在某一时刻状态的快照且每一个栈跟踪信息 会在不同的时间得到.
	     * 如果虚拟机中某一个线程没有栈跟踪信息则其数组长度为0.
	     * 如果有安全管理器,则安全管理器的checkPermission方法会被调用以检查是否允许获取所有线程的栈跟踪信息.
	     */
	    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();
	
	    /**
	     * 获取当前线程,线程id
	     */
	    public long getId() {
	        return tid;
	    }
	
	    /* 线程状态枚举  */
	    public enum State {

	         // 新建窗台,创建后还没有执行start方法的线程
	        NEW,
	        
	         //  运行中状态,包含在运行中的线程以及在等待CPU调度的线程
	        RUNNABLE,

	         // 阻塞状态,表示线程阻塞于锁
	        BLOCKED,
	
	        // 等待状态,一个线程处于wating状态,是因为调用了下面方法中的某一个: 1.Object.wait 2.Thread.join 3.LockSupport.park
	        WAITING,
	
	        /**
	         *等待超时状态 线程等待指定时间. 这种状态的出现是因为调用了下面方法中的某一个:
	         	 1.Thread.sleep() 2.Object.wait() 3.Thread.join() 4.LockSupport.parkNanos(), 5.LockSupport.parkUntil()
	         */
	        TIMED_WAITING,
	
	        // 终止状态,表示当前线程已经执行完毕。
	        TERMINATED;
	    }

	     /*返回线程状态. 这一方法的设计目的:用于系统状态的监听,而非同步控制.  */
	    public State getState() {
	        // get current thread state
	        return sun.misc.VM.toThreadState(threadStatus);
	    }
	
	    // Added in JSR-166
	
	    /**
	     * 由于未捕获异常而导致线程终止的函数接口处理器. 当一个线程由于未捕获异常而终止时,JVM将会使用getUncaughtExceptionHandler来查询此线程的UncaughtExceptionHandler,
	     * 且会调用处理器handler的uncaughtException()方法,将此线程和其异常作为参数.
	     * 如果一个线程没有它特定的UncaughtExceptionHandler,则它所属的线程组对象充当其UncaughtExceptionHandler.
	     * 如果线程组对象没有处理异常的指定请求,它可以向前调用getDefaultUncaughtExceptionHandler的默认处理异常的方法.
	     */
	    @FunctionalInterface
	    public interface UncaughtExceptionHandler {
	        /**
	         * 由于未捕获异常而导致线程终止的方法调用. 此方法抛出的任何异常都会被JVM忽略.
	         */
	        void uncaughtException(Thread t, Throwable e);
	    }
	
	    // null unless explicitly set
	    private volatile UncaughtExceptionHandler uncaughtExceptionHandler;
	
	    // null unless explicitly set
	    private static volatile UncaughtExceptionHandler defaultUncaughtExceptionHandler;
	
	    /**
	     * 设定默认处理器用于处理:由于未捕获异常而导致的线程终止,且此线程还未定义任何其它的处理器.
	     * 未捕获异常首先由本线程进行处理,然后由线程所属的线程组对象处理,最后由默认未捕获异常处理器进行处理.
	     * 如果线程未设定明确的未捕获异常处理器,且线程的线程组(包括父线程组)也未指定,则此时默认处理器的uncaughtException
	     * 方法会被执行.由于设定了默认的未捕获异常处理器,则应用能够更改未捕获异常的处理方法.
	     * 注意:默认的未捕获异常处理器不应该经常使用线程的线程组对象,因为这会引起无限递归.
	     */
	    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;
	    }
	
	    //返回由于未捕获异常而导致线程中断的处理器.如果此线程无未捕获异常处理器,则返回此线程的线程组对象
	    //如果此线程已经终止,则返回null.
	    public UncaughtExceptionHandler getUncaughtExceptionHandler() {
	        return uncaughtExceptionHandler != null ? uncaughtExceptionHandler : group;
	    }
	
	    /**
	     * 设定一个由于未捕获异常而导致线程中断的处理器. 
	     * 通过设定未捕获异常处理器,一个线程可以完全控制如何处理未捕获异常.
	     * 如果没有设定未捕获异常,则线程组对象默认为其未捕获异常处理器.
	     */
	    public void setUncaughtExceptionHandler(UncaughtExceptionHandler eh) {
	        checkAccess();
	        uncaughtExceptionHandler = eh;
	    }
	
	    //将未捕获异常分发给处理器.这一方法通常被JVM调用.
	    private void dispatchUncaughtException(Throwable e) {
	        getUncaughtExceptionHandler().uncaughtException(this, e);
	    }
	
	    //删除指定map中那些在特定引用队列中已经排队的所有key
	    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.
     *  WeakReference类说明:弱引用对象(JVM四种引用中的弱引用),这并不妨碍它们的引用对象被最终化、定型,然后回收.弱引用最常用于实现规范化映射。
     **/
	    static class WeakClassKey extends WeakReference<Class<?>> {
	        /**
	         * saved value of the referent's identity hash code, to maintain a consistent hash code after the referent has
	         * been cleared
	         */
	        private final int hash;
	
	        /**
	         * Create a new WeakClassKey to the given object, registered with a queue.
	         */
	        WeakClassKey(Class<?> cl, ReferenceQueue<Class<?>> refQueue) {
	            super(cl, refQueue);
	            hash = System.identityHashCode(cl);
	        }
	
	        /**
	         * Returns the identity hash code of the original referent.
	         */
	        @Override
	        public int hashCode() {
	            return hash;
	        }
	
	        /**
	         * Returns true if the given object is this identical WeakClassKey instance, or, if this object's referent has
	         * not been cleared, if the given object is another WeakClassKey instance with the identical non-null referent
	         * as this one.
	         */
	        @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 following three initially uninitialized fields are exclusively
	    // managed by class java.util.concurrent.ThreadLocalRandom. These
	    // fields are used to build the high-performance PRNGs in the
	    // concurrent code, and we can not risk accidental false sharing.
	    // Hence, the fields are isolated with @Contended.
	
	    /** 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);
	}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Starry-Leo

帮到了您,有闲钱,再打赏哦~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值