Java中Thread源码剖析(一)

2014-11-24 00:07:49 · 作者: · 浏览: 2

关于线程,用很长时间了,主线程下的子线程去做一些事情,就是一个代理模式,主线程分代理权给子线程,子线程帮主线程完成一些任务。今天我们来看下线程的源码,进行系统的学习。

1、首先线程有六种状态

public enum State {
/**
* The thread has been created, but has never been started.
*/
NEW,
/**
* The thread may be run.
*/
RUNNABLE,
/**
* The thread is blocked and waiting for a lock.
*/
BLOCKED,
/**
* The thread is waiting.
*/
WAITING,
/**
* The thread is waiting for a specified amount of time.
*/
TIMED_WAITING,
/**
* The thread has been terminated.
*/
TERMINATED
}

NEW:刚创建还没启动

RUNNABLE:可以执行

BLOCKED:堵塞状态,等待持有锁

WAITING :处理等待状态

TIMED_WAITING:等待一些时间

TERMINATED:终止

/**
* The maximum priority value allowed for a thread.
*/
public static final int MAX_PRIORITY = 10;


/**
* The minimum priority value allowed for a thread.
*/
public static final int MIN_PRIORITY = 1;


/**
* The normal (default) priority value assigned to threads.
*/
public static final int NORM_PRIORITY = 5;

2、分别是线程可设置的最大、最小和默认优先级,级别越高执行越靠前

/**
* Holds the thread's ID. We simply count upwards, so
* each Thread has a unique ID.
*/
private long id;

3、每个线程都有一个独一无二的ID

public Thread() {
create(null, null, null, 0);
}

private void create(ThreadGroup group, Runnable runnable, String threadName, long stackSize) {
Thread currentThread = Thread.currentThread();
if (group == null) {
group = currentThread.getThreadGroup();
}


if (group.isDestroyed()) {
throw new IllegalThreadStateException(Group already destroyed);
}


this.group = group;


synchronized (Thread.class) {
id = ++Thread.count;
}


if (threadName == null) {
this.name = Thread- + id;
} else {
this.name = threadName;
}


this.target = runnable;
this.stackSize = stackSize;


this.priority = currentThread.getPriority();


this.contextClassLoader = currentThread.contextClassLoader;


// Transfer over InheritableThreadLocals.
if (currentThread.inheritableva lues != null) {
inheritableva lues = new ThreadLocal.Values(currentThread.inheritableva lues);
}
// add ourselves to our ThreadGroup of choice
this.group.addThread(this);
}

4、初始化一个空的线程,获得当前运行的线程群组,设置id,name,执行线程runnable,池大小stackSize,优先级,并加入到线程群组,这是一个无参的Thread,正常情况下应该有ThreadGroup、Runnable、threadName和stackSize这四个参数。一般threadName如果为空,则报出NullPointerException,stackSize默认为0。

/**
* Destroys the receiver without any monitor cleanup.
*
* @deprecated Not implemented.
*/
@Deprecated
public void destroy() {
throw new NoSuchMethodError(Thread.destroy()); // TODO Externalize
}

5、destroy方法在java7已经被抛弃。

public void interrupt() {
synchronized (interruptActions) {
for (int i = interruptActions.size() - 1; i >= 0; i--) {
interruptActions.get(i).run();
}
}

VMThread vmt = this.vmThread;
if (vmt != null) {
vmt.interrupt();
}
}

6、停止当前线程,如果线程处于wait、join、sleep状态的线程,会报异常。

public final boolean isAlive() {
return (vmThread != null);
}

7、线程是否死掉,主要是判断虚拟机线程有没有死掉VMThread,当然获得当前线程也是通过VMThread.currentThread(),以及接下下获得当前线程处于六大状态中的哪种。

public State getState() {
// TODO This is ugly and should be implemented better.
VMThread vmt = this.vmThread;


// Make sure we have a valid reference to an object. If native code
// deletes the reference we won't run into a null reference later.
VMThread thread = vmThread;
if (thread != null) {
// If the Thread Object became invalid or was not yet started,
// getStatus() will return -1.
int state = thread.getStatus();
if(state != -1) {
return VMThread.STATE_MAP[state];
}
}
return hasBeenStarted Thread.State.TERMINATED : Thread.State.NEW;
}

public final void join() throws InterruptedException {
VMThread t = vmThread;
if