无论我们是在学习OS中了解的线程还是在多线程的学习中了解的线程,都会学习到一个共同的知识点,就是线程的生命周期或者是线程的状态,这里通过Java语言对线程进行详解。

在Java语言中,我们对Thread类肯定是非常熟悉的,那大家知道在Java语言中,线程的状态在哪里定义的吗?

在Java中,线程的状态是通过内部枚举类实现的,无论我们通过API或者是源码我们看到,线程有6中状态,分别是NEW,RUNNABLE,WAITING,TIMED_WAITING、BLOCKED、TERMINATED。

java线程通知 java 线程信息_线程状态

public enum State {
        /**
         * Thread state for a thread which has not yet started.
         */
        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 <tt>Object.wait()</tt>
         * on an object is waiting for another thread to call
         * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
         * that object. A thread that has called <tt>Thread.join()</tt>
         * 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;
    }

这里我们再看一个图,可以看到,我们调用具体的方法后,都会改变线程的状态,下面通过具体的代码去验证每一个状态。

java线程通知 java 线程信息_Java_02

线程状态之New

线程实例创建后,未调用start()方法,处于new状态

package chapter7.threadState;

public class StateTest {
	public static void main(String[] args) {
		Thread t1 = new Thread(() -> {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		},"A");
		
		System.out.println("调用start方法之前的线程状态:" + t1.getState());
		//t1.start();
		//System.out.println("调用start方法之后的线程状态:" + t1.getState());
	}
}

java线程通知 java 线程信息_java线程通知_03

线程状态之Runnable

调用start方法后,线程就从new状态转变为runnable状态了

package chapter7.threadState;

public class StateTest {
	public static void main(String[] args) {
		Thread t1 = new Thread(() -> {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		},"A");
		//System.out.println("调用start方法之前的线程状态:" + t1.getState());
		t1.start();
		System.out.println("调用start方法之后的线程状态:" + t1.getState());
	}
}

java线程通知 java 线程信息_System_04

线程状态之Waiting

调用wait,join等方法线程进入waiting状态

package chapter7.threadState;

public class StateTest1 {
	public static void main(String[] args) {
		Thread t1 = new Thread(() -> {
			Object obj = new Object();
			synchronized (obj) {
				try {
					obj.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		},"A");
		
		t1.start();
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("调用wait等方法之后的线程状态:" + t1.getState());
	}
}

java线程通知 java 线程信息_System_05

线程状态之TIMED_WAITING

调用wait(long times)等方法线程进入TIMED_WAITING状态

package chapter7.threadState;

public class StateTest1 {
	public static void main(String[] args) {
		Thread t1 = new Thread(() -> {
			Object obj = new Object();
			synchronized (obj) {
				try {
					obj.wait(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		},"A");
		
		t1.start();
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("调用wait(long times)等方法之后的线程状态:" + t1.getState());
	}
}

java线程通知 java 线程信息_System_06

线程状态之Blocked

在多线程情况下,B线程在等待A线程运行的过程中,线程B处于Blocked状态

package chapter7.threadState;

public class StateTest2 {
	public static void main(String[] args) {
		Service service = new Service();
		Thread t1 = new Thread(() -> {
			service.method();
		},"A");
		Thread t2 = new Thread(() -> {
			service.method();
		},"B");
		
		t1.start();
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		t2.start();
		System.out.println("线程A的状态" + t1.getState());
		System.out.println("线程B的状态" + t2.getState());
	}
}

class Service{
	synchronized public void method(){
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

java线程通知 java 线程信息_System_07

线程状态之TERMINATED

线程在运行结束之后是处于TERMINATED的,一旦线程处于这种状态,是无法再次运行的,面临着死亡回收。

package chapter7.threadState;

public class StateTest3 {
	public static void main(String[] args) {
		Thread t1 = new Thread(() -> {
			System.out.println("线程正在运行中");
		});
		
		t1.start();
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("线程运行结束状态为:" + t1.getState());
	}
}

java线程通知 java 线程信息_System_08


线程状态的学习就到这里,只有真正理解线程在运行过程中的各种状态,才可以在多线程的学习中如鱼得水般自由“翱翔”。