以下是 JDK 8 中 Thread
类的部分源代码:
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;
private boolean stillborn = false;
private Runnable target;
private ThreadGroup group;
private ClassLoader contextClassLoader;
private AccessControlContext inheritedAccessControlContext;
private static int threadInitNumber;
ThreadLocal.ThreadLocalMap threadLocals = null;
ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
private long stackSize;
private boolean started = false;
private boolean interrupted = false;
private static long threadSeqNumber;
private volatile int threadStatus = 0;
private static synchronized long nextThreadID() {
return ++threadSeqNumber;
}
public Thread() {
this(null, null, "Thread-" + nextThreadID(), 0);
}
public Thread(Runnable target) {
this(null, target, "Thread-" + nextThreadID(), 0);
}
public Thread(ThreadGroup group, Runnable target) {
this(group, target, "Thread-" + nextThreadID(), 0);
}
public Thread(String name) {
this(null, null, name, 0);
}
public Thread(ThreadGroup group, String name) {
this(group, null, name, 0);
}
public Thread(Runnable target, String name) {
this(null, target, name, 0);
}
public Thread(ThreadGroup group, Runnable target, String name) {
this(group, target, name, 0);
}
public Thread(ThreadGroup group, Runnable target, String name,
long stackSize) {
init(group, target, name, stackSize);
}
private void init(ThreadGroup g, Runnable target, String name,
long stackSize) {
if (name == null) {
throw new NullPointerException("name cannot be null");
}
this.name = name;
Thread parent = currentThread();
this.group = (g != null) ? g : parent.getThreadGroup();
this.target = target;
this.priority = parent.getPriority();
this.daemon = parent.isDaemon();
this.contextClassLoader = parent.getContextClassLoader();
this.inheritedAccessControlContext = AccessController.getContext();
this.stackSize = stackSize;
setPriority(priority);
if (parent.inheritableThreadLocals != null) {
this.inheritableThreadLocals =
ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
}
this.stackSize = stackSize;
}
public synchronized void start() {
if (threadStatus != 0)
throw new IllegalThreadStateException();
group.add(this);
started = false;
try {
start0();
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
}
}
}
private native void start0();
public void run() {
if (target != null) {
target.run();
}
}
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);
}
}
private native void setPriority0(int newPriority);
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);
}
}
private native void setNativeName(String name);
public final void setDaemon(boolean on) {
checkAccess();
if (isAlive()) {
throw new IllegalThreadStateException();
}
daemon = on;
}
public final void join() throws InterruptedException {
join(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;
}
}
}
public static native void yield();
public static native void sleep(long millis) throws InterruptedException;
public static native void sleep(long millis, int nanos) throws InterruptedException;
public static native int activeCount();
public static native int enumerate(Thread tarray[]);
public native int countStackFrames();
public synchronized void stop() {
stop(new ThreadDeath());
}
public synchronized void stop(Throwable obj) {
checkAccess();
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPermission(SecurityConstants.STOP_THREAD_PERMISSION);
if (this != Thread.currentThread()) {
security.checkPermission(SecurityConstants.STOP_OTHER_THREAD_PERMISSION);
}
}
if (obj == null) {
throw new NullPointerException();
}
stop0(new ThreadDeath());
}
private native void stop0(Object obj);
public void interrupt() {
if (this != Thread.currentThread()) {
checkAccess();
}
interrupt0();
}
public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
public boolean isInterrupted() {
return isInterrupted(false);