目录
线程的六种状态
很多文章在介绍线程的状态时候都有一个错误的说法-存在RUNNING状态,实际上在线程的状态中是没有RUNNING的,他的状态如下图所示:
如何证明是这集中状态呢?我们可以直接看源码
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;
}
NEW
线程创建后的未调用start方法前的状态是NEW
RUNNABLE
当线程调用start()方法,线程的状态就是RUNNABLE,表示线程已经准备就绪可以被执行了,如果CPU给该现场分配时间片,那么现场就被执行;RUNNABLE表示准备就绪,不是被执行。
BLOCKED
当线程需要进入临界区,但是前面有一个线程临界区代码正在被执行,此时现场就进入blocked状态,表示现场阻塞。
WAITING
当线程在临界区内调用wait()方法,注意wait()方法必须在临界区内执行;或者调用jion方法,线程就进入等待状态,表示现场等待其他线程执行一个特殊操作,唤醒该线程
TIME_WAITING
当线程调用一下方法是线程就进入超时等待状态,如果达到超时条件其他现场还没有唤起当前线程时,当前现场则会被计时器唤起。
- Thread.sleep
- Object.wait with timeout
- Thread.join with timeout
- LockSupport.parkNanos
- LockSupport.parkUntil
TERMINATED
终结状态,线程正常或异常结束
sleep与yield区别
sleep | yield | |
线程状态 | TIME_WAITING | RUNNABLE |
监视器锁 | 不释放 | 不释放 |
暂停时间 | 有参数执行 | 不可以指定 |
CPU资源 | 执行完sleep后和其他线程一起竞争CPU资源 | CPU资源让渡给相同优先级线程, 如果没有则由当前线程再次执行 |
测试代码
public class ThreadDemo {
private static final Object monitor = new Object();
public static void main(String[] args) {
Thread threadA = new Thread(() -> {
try {
TimeUnit.SECONDS.sleep(2);
synchronized (monitor) {
//进入阻塞状态
monitor.wait(4000);
//被唤醒后线程状态是RUNABLE
printThreadStates("线程被唤醒后", Thread.currentThread());
monitor.wait();
try {
TimeUnit.SECONDS.sleep(30);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}, "A");
//新创建一个线程时,它的状态就是NEW
printThreadStates("新创建线程", threadA);
//启动线程以后线程就是RUNNABLE,可以运行不代表已经正在运行;只是表示这个线程可以被CPU调度
threadA.start();
printThreadStates("启动线程", threadA);
try {
TimeUnit.SECONDS.sleep(3);
} catch (Exception e) {
e.printStackTrace();
}
printThreadStates("线程超时等待", threadA);
synchronized (monitor) {
monitor.notifyAll();
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
printThreadStates("线程等待中", threadA);
synchronized (monitor) {
monitor.notifyAll();
}
Thread threadB = new Thread(() -> {
synchronized (monitor) {
try {
TimeUnit.SECONDS.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
}
}, "B");
threadB.start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
printThreadStates("获取锁中", threadB);
Thread threadC = new Thread(() -> {
try {
threadB.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}, "C");
threadC.start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
printThreadStates("join", threadC);
Thread threadD = new Thread(() -> {
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}, "D");
threadD.start();
try {
TimeUnit.SECONDS.sleep(30);
} catch (Exception e) {
e.printStackTrace();
}
printThreadStates("sleep", threadD);
printThreadStates("线程等待中", threadA);
}
private static void printThreadStates(String prefix, Thread thread) {
System.out.println(prefix + " , " + thread.getName() + " : " + thread.getState());
}
}
执行结果
新创建线程 , A : NEW
启动线程 , A : RUNNABLE
线程超时等待 , A : TIMED_WAITING
线程被唤醒后 , A : RUNNABLE
线程等待中 , A : WAITING
获取锁中 , B : BLOCKED
join , C : WAITING
sleep , D : TIMED_WAITING
线程等待中 , A : TERMINATED