前言:网络上的文章杂乱无章,有的说线程是5种也有的说是6种状态,这里我们给出答案:6种
6种线程状态
java.lang.Thread类中明确声明了线程的6种状态,查看Thread的State枚举类,分别是NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED
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;
}
那线程的5种状态又是从何而来?答案是:5种状态是针对OS操作系统的线程状态,分别是NEW, RUNNABLE, RUNNING, BLOCKED, DEAD
Java线程将操作系统线程状态的RUNNABLE, RUNNING, BLOCKED合并为一个状态:Runnable状态,具体如下图:
现在对这些状态分别测试一下,创建6个线程,分别让其处于特定的状态
public class MyThread {
public static void main(String[] args) throws InterruptedException {
//NEW
Thread t1 = new Thread(()->{
}, "t1");
//RUNNABLE
Thread t2 = new Thread(()->{
while (true) {
}
});
t2.start();
//WAITING
Thread t3 = new Thread(()->{
synchronized (MyThread.class){
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t3.start();
//TIMED_WAITING
Thread t4 = new Thread(()->{
try { Thread.sleep(1000000); } catch (InterruptedException e) { e.printStackTrace(); }
});
t4.start();
//BLOCKED
Thread t5 = new Thread(()->{
synchronized (MyThread.class){
}
});
t5.start();
//TERMINATED
Thread t6 = new Thread(()->{
});
t6.start();
Thread.sleep(1000);
System.out.println(t1.getState());
System.out.println(t2.getState());
System.out.println(t3.getState());
System.out.println(t4.getState());
System.out.println(t5.getState());
System.out.println(t6.getState());
}
}
输出结果如下
NEW
RUNNABLE
WAITING
TIMED_WAITING
BLOCKED
TERMINATED
至于各个状态是如何相互转换的,下篇文章再谈。