Thread源码剖析

使用

  • 查看线程名:Thread.currenThread().getName();一般来说,主线程叫做main其他线程是Thread-x

  • 修改线程名:setName(String name)

  • 守护线程是为其他线程服务的

    • 垃圾回收线程就是守护线程
    • 特点:当别的用户线程执行完了,虚拟机就会退出,守护线程也就会被停止掉了。
    • 使用方法setDaemon(boolean on )在线程启动前设置为守护线程
    • 守护线程不要访问共享资源(数据库、文件等),因为它可能在任何时候结束掉
    • 守护线程中产生的新线程也是守护线程。
  • 优先级线程:线程优先级高,仅仅表示线程获取的CPu时间片的几率高,但这不是一个确定的因素(高度依赖于操作系统)

  • 线程的生命周期

    点击查看源网页

  • sleep方法:会进入计时等待状态、等时间到了,进入的是就绪状态而非运行状态。

  • yield方法:先让别的线程执行,但是不确保真正让出。

  • join方法:会等待该线程执行完毕后才执行别的线程。

  • interrupt方法:请求终止线程,不会真正停止一个线程,只是告诉它要结束了,想让线程自己来终止,具体中断还是继续执行应该由被通知的线程自己处理。

    • 方法中另外两个方法(检查该线程是否被中断):
      • 静态方法interrupted()——会清除中断标志位
      • 实例方法isInterrupted()——不会清楚中断标志位
    • stop和interrupt区别
      • stop:可以让一个线程A终止掉另一个线程B,太暴力,且过时
        • 被终止的线程B会立即释放锁,这可能会让对象处于不一致的状态中
        • 线程A也不知道B什么时候能够被终止掉
      • interrupt:并不是要真正终止掉当前线程,仅仅是设置了一个中断标志。这个中断标志可以给我们用来判断什么时候该干什么活,什么时候中断由我们自己来决定,这样可以安全地终止线程。
        • 此方法根本不会对线程的状态造成影响,它仅仅设置了一个标志位

源码

package java.lang;

import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.security.AccessController;
import java.security.AccessControlContext;
import java.security.PrivilegedAction;
import java.util.Map;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.LockSupport;
import sun.nio.ch.Interruptible;
import sun.reflect.CallerSensitive;
import sun.reflect.Reflection;
import sun.security.util.SecurityConstants;

public
class Thread implements Runnable {
    
    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;

 
    private boolean     stillborn = false;

   
    private Runnable target;

  
    private ThreadGroup group;

   
    private ClassLoader contextClassLoader;

   
    private AccessControlContext inheritedAccessControlContext;

    
    private static int threadInitNumber;
    //从0开始加
    private static synchronized int nextThreadNum() {
        return threadInitNumber++;
    }

    
    ThreadLocal.ThreadLocalMap threadLocals = null;

    
    ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;

    
    private long stackSize;

   
    private long nativeParkEventPointer;

   
    private long tid;

   
    private static long threadSeqNumber;

    
    private volatile int threadStatus = 0;


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


    volatile Object parkBlocker;


    private volatile Interruptible blocker;
    private final Object blockerLock = new Object();


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


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

   	/*
   	 *初始化线程
   	 *
   	 */
    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();
            }
        }

      
        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);
        
        this.stackSize = stackSize;

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

    public synchronized void start() {
       
        if (threadStatus != 0)
            throw new IllegalThreadStateException();

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

   
    @Override
    public void run() {
        if (target != null) {
            target.run();
        }
    }

    
    private void exit() {
        if (group != null) {
            group.threadTerminated(this);
            group = null;
        }
        
        target = null;
       
        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);
            }
        }
       
        if (threadStatus != 0) {
            resume(); 
        }

        stop0(new ThreadDeath());
    }

    
    @Deprecated
    public final synchronized void stop(Throwable obj) {
        throw new UnsupportedOperationException();
    }
	/*
	 * 中断当前线程
	 * 只能自己调用中断的方法,不然会抛出安全异常
	 */
    public void interrupt() {
        //检查是否有权限
        if (this != Thread.currentThread())
            checkAccess();

        synchronized (blockerLock) {
            //看看是不是阻塞的线程调用
            interruptible b = blocker;
            if (b != null) {
                interrupt0();
                //如果是,抛出异常,将中断标志位改了(改为false)一般我们会再catch中再次修改
                b.interrupt(this);
                return;
            }
        }
        //如果很顺利,就可以修改到标志位(没有进入if)
        interrupt0();
    }
	//该方法会清除中断标志位
    public static boolean interrupted() {
        return currentThread().isInterrupted(true);
    }
	//该方法不会影响中断标志位
    public boolean isInterrupted() {
        return isInterrupted(false);
    }

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

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

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

    public static void dumpStack() {
        new Exception("Stack trace").printStackTrace();
    }
	//如果启动了再设置守护线程,会抛出异常
    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;
    }

    public static native boolean holdsLock(Object obj);

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

    public StackTraceElement[] getStackTrace() {
        if (this != Thread.currentThread()) {
          
            SecurityManager security = System.getSecurityManager();
            if (security != null) {
                security.checkPermission(
                    SecurityConstants.GET_STACK_TRACE_PERMISSION);
            }
           
            if (!isAlive()) {
                return EMPTY_STACK_TRACE;
            }
            StackTraceElement[][] stackTraceArray = dumpThreads(new Thread[] {this});
            StackTraceElement[] stackTrace = stackTraceArray[0];
            
            if (stackTrace == null) {
                stackTrace = EMPTY_STACK_TRACE;
            }
            return stackTrace;
        } else {
            
            return (new Exception()).getStackTrace();
        }
    }

    public static Map<Thread, StackTraceElement[]> getAllStackTraces() {
        
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkPermission(
                SecurityConstants.GET_STACK_TRACE_PERMISSION);
            security.checkPermission(
                SecurityConstants.MODIFY_THREADGROUP_PERMISSION);
        }

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


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

  
    private static class Caches {
      
        static final ConcurrentMap<WeakClassKey,Boolean> subclassAudits =
            new ConcurrentHashMap<>();

       
        static final ReferenceQueue<Class<?>> subclassAuditsQueue =
            new ReferenceQueue<>();
    }

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

    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() {
        
        return sun.misc.VM.toThreadState(threadStatus);
    }

  
    @FunctionalInterface
    public interface UncaughtExceptionHandler {
        
        void uncaughtException(Thread t, Throwable e);
    }

    
    private volatile UncaughtExceptionHandler uncaughtExceptionHandler;


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

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


    @sun.misc.Contended("tlr")
    long threadLocalRandomSeed;

   
    @sun.misc.Contended("tlr")
    int threadLocalRandomProbe;

  
    @sun.misc.Contended("tlr")
    int threadLocalRandomSecondarySeed;

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

测试实例

给线程起名字
  • 实现了Runnable的方式来实现多线程
public class MyRunnable implements Runnable {
    @Override
    public void run() {
        //打印出当前线程的名字
        System.out.println(Thread.currentThread().getName());
    }
}

  • 测试
public class MyRunnableDemo {
    public static void main(String[] args) {
        // 创建MyRunnable类的对象
        MyRunnable my = new MyRunnable();
        // 使用带参构造方法给线程起名字
        Thread t1 = new Thread(my,"关注我");
        Thread t2 = new Thread(my,"冢狐");
        t1.start();
        t2.start();
        // 打印出当前线程的名字
        System.out.println(Thread.currentThread().getName());
    }
}
  • 结果

守护线程
  • 设置守护线程
public class MyRunnableDemo {
    public static void main(String[] args) {
        // 创建MyRunnable类的对象
        MyRunnable my = new MyRunnable();
        // 使用带参构造方法给线程起名字
        Thread t1 = new Thread(my,"关注我");
        Thread t2 = new Thread(my,"冢狐");
        //设置守护线程
        t2.setDaemon(true);
        t1.start();
        t2.start();
        // 打印出当前线程的名字
        System.out.println(Thread.currentThread().getName());
    }
}
  • 结果

线程1和主线程执行完了,我们的守护线程就不执行了,所以要在启动前设置守护线程

线程停止
public class Main {
    public static void main(String[] args) {
        Main main=new Main();
        //创建线程并启动
        Thread t=new Thread(main.runnable);
        System.out.println("This is main");
        t.start();
        try {
            // 在main线程睡个三秒钟
            Thread.sleep(3000);
        }catch (InterruptedException e){
            System.out.println("In main");
            e.printStackTrace();
        }
        // 设置中断
        t.interrupt();
    }
    Runnable runnable=()->{
        int i=0;
        try {
            while (i<1000){
                // 睡个半秒再执行
                Thread.sleep(500);
                System.out.println(i++);
            }
        }catch (InterruptedException e){
            // 判断该阻塞线程是否还在
            System.out.println(Thread.currentThread().isAlive());
            // 判断该线程的中断标志位状态
            System.out.println(Thread.currentThread().isInterrupted());

            System.out.println("In Runnable");
            e.printStackTrace();
        }
    };
}
  • 结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值