Thread类位于java.lang包下,在Thread类中有一个枚举类型State,State中定义了线程的六种状态。源码如下:
java.lang.Thread.State
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; }
6中线程状态:
注:线程在给定时间点只能处于一种状态。这些状态是虚拟机状态,不反映任何操作系统线程状态。
调用getState
方法可以得到当前线程的状态。
public class ThreadTest { public static void main(String[] args) { Thread thread1 = new MyThread(); System.out.println("thread1的线程状态:" + thread1.getState()); thread1.start(); System.out.println("thread1的线程状态:" + thread1.getState()); } } class MyThread extends Thread{ @Override public void run() { System.out.println(this.getName() + " is running。。。"); } }
public class ThreadState { public static void main(String[] args) throws InterruptedException { Thread thread1 = new Thread(new Runnable() { @Override public void run() { System.out.println("当前线程正在执行-------"); System.out.println("线程正在执行时的线程状态:" + Thread.currentThread().getState().toString()); } }); System.out.println("创建线程后,调用start前的线程状态:" + thread1.getState().toString()); thread1.start(); Thread.sleep(2000L); // 等待thread1执行结束,再看状态 System.out.println("---等待两秒,线程执行结束后的状态:" + thread1.getState().toString()); //thread1.start(); //当线程终止之后,再进行调用,会抛出IllegalThreadStateException异常 } }
程序运行结果显示:
注意:当线程执行结束之后,再进行调用,会抛出IllegalThreadStateException异常
public class ThreadState { public static void main(String[] args) throws InterruptedException { Thread thread2 = new Thread(new Runnable() { @Override public void run() { System.out.println("当前线程正在执行-------,需要花费两秒时间------"); try { // 将线程2移动到等待状态,2s后自动唤醒 Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程正在执行时的线程状态:" + Thread.currentThread().getState().toString()); } }); System.out.println("创建线程后,调用start前的线程状态:" + thread2.getState().toString()); thread2.start(); Thread.sleep(200L); // 等待200毫秒,再看状态 System.out.println("等待200毫秒(线程还没执行结束),线程的状态:" + thread2.getState().toString()); Thread.sleep(3000L); // 再等待3秒,让thread2执行完毕,再看状态 System.out.println("等待3秒(线程执行结束),线程的状态:" + thread2.getState().toString()); } }
程序运行结果显示:
public class ThreadState { public static void main(String[] args) throws InterruptedException { Thread thread3 = new Thread(new Runnable() { @Override public void run() { synchronized (ThreadState.class) { System.out.println("当前线程正在执行-------"); System.out.println("线程正在执行时的线程状态:" + Thread.currentThread().getState().toString()); } } }); synchronized (ThreadState.class) { System.out.println("没调用start方法,thread3当前状态:" + thread3.getState().toString()); thread3.start(); System.out.println("调用start方法,thread3当前状态:" + thread3.getState().toString()); Thread.sleep(200L); System.out.println("等待200毫秒,线程遇到锁被堵塞时的状态:" + thread3.getState().toString()); } Thread.sleep(3000L); // 再等待3秒,让thread3执行完毕,再看状态 System.out.println("等待3秒,线程抢到锁并执行结束时的状态:" + thread3.getState().toString()); } }
程序运行结果显示: