Thread源码分析

June 09, 2023 作者: dyzmj 分类: 浏览: 4 评论: 0

Thread

Thread 类

Thread 类的定义

public class Thread implements Runnable {}

加载本地资源

private static native void registerNatives();
    static {
        registerNatives();
    }

Thread 类中的成员变量

// 当前线程的名称
private volatile String name;
// 线程的优先级
private int priority;

// 当前线程是否是守护线程
private boolean daemon = false;

// JVM独占使用的字段
private boolean stillborn = false;
private long eetop;

// 实际执行的任务
private Runnable target;

// 当前线程所在的线程组
private ThreadGroup group;

// 当前线程的类加载器
private ClassLoader contextClassLoader;

// 当前线程继承的访问控制上下文
private AccessControlContext inheritedAccessControlContext;

// 为匿名线程生成的自动编号
private static int threadInitNumber;
private static synchronized int nextThreadNum() {
    return threadInitNumber++;
}

// 与当前线程相关的ThreadLocal,这个Map维护的是ThreadLocal类
ThreadLocal.ThreadLocalMap threadLocals = null;

// 与当前线程相关的ThreadLocal,
ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;

// 当前线程请求的堆栈大小,若未指定,则默认为0,并由JVM来控制
private final long stackSize;

// 当前线程终止后存在的JVM私有状态
private long nativeParkEventPointer;

// 线程ID
private final long tid;

// 用于生成线程ID的参数
private static long threadSeqNumber;

// 线程状态,初始为未启动状态
private volatile int threadStatus;

// 提供给当前调用java.util.concurrent.locks.LockSupport.park的参数
volatile Object parkBlocker;

// Interruptible接口中定义了interrupt方法,用来中断指定的线程
private volatile Interruptible blocker;
// 当前线程的内部锁
private final Object blockerLock = new Object();

// 线程的最小优先级
public static final int MIN_PRIORITY = 1;
// 线程默认优先级
public static final int NORM_PRIORITY = 5;
// 线程最大优先级
public static final int MAX_PRIORITY = 10;

线程的状态定义

public enum State {
        // 未启动状态
        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 {@code Object.wait()}
         * on an object is waiting for another thread to call
         * {@code Object.notify()} or {@code Object.notifyAll()} on
         * that object. A thread that has called {@code Thread.join()}
         * is waiting for a specified thread to terminate.
         */
        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>
         */
        TIMED_WAITING,

        /**
         * Thread state for a terminated thread.
         * The thread has completed execution.
         */
        TERMINATED;
    }
#Thread(3)

评论