java Thread类中定义了线程的六种状态:NEW、RUNNABLE、BLOCKED、WAITING、TIMED_WAITING、TERMINATED
public enum State {
NEW,
RUNNABLE,
BLOCKED,
WAITING,
TIMED_WAITING,
TERMINATED;
}
- NEW、RUNNABLE、TERMINATED
- 新建还没有Start的线程状态为NEW
- 线程本身没有被其他线程阻塞,等待cpu调度或者用户io的线程状态为RUNNABLE
- 运行结束状态为TERMINATED
示例:
public class NewRunAndTerminate {
public static void main(String args[]) throws InterruptedException {
Thread firstThread = new TestThread(); //刚创建时是new
System.out.println(firstThread.getState());
firstThread.start();
System.out.println(firstThread.getState());//start 之后是RUNNABLE
firstThread.join();
System.out.println(firstThread.getState());//结束后是TERMINATED
}
}
class TestThread extends Thread {
@Override
public void run() {
System.out.println("Thread start");
System.out.println("Thread end");
}
}
运行结果
NEW
RUNNABLE
Thread start
Thread end
TERMINATED
Process finished with exit code 0
- WAITING和TIMED_WAITING
- 当线程调用 join 、 wait 或者 sleep 方法时,状态会变成WAITING或者TIMED_WAITING
- WAITING和TIMED_WAITING的区别在于有没有设置超时时间,如果设置了超时时间就是TIMED_WAITING,没设置超时时间就是WAITING
具体可以看以下两个例子:
1)不设超是时间
public class TimeWaitAndWait {
public static void main(String args[]) throws InterruptedException {
Object lock = new Object();
ThreadA a = new ThreadA(lock);
ThreadB b = new ThreadB(lock);
a.start();
Thread.sleep(1000);
b.start();
//启动AB两个线程,打印状态
while (a.isAlive() || b.isAlive()) {
System.out.println("Thread A:" + a.getState() + ",Thread B:" + b.getState());
Thread.sleep(500);
}
}
}
//线程A
class ThreadA extends Thread {
private Object lock;
public ThreadA(Object lock) {
this.lock = lock;
}
@Override
public void run() {
System.out.println("ThreadA started.");
synchronized (lock) {
System.out.println("ThreadA get the lock.");
try {
lock.wait(); //wait,等待线程B唤醒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("ThreadA release the lock.");
System.out.println("ThreadA end.");
}
}
class ThreadB extends Thread {
private Object lock;
public ThreadB(Object lock) {
this.lock = lock;
}
@Override
public void run() {
System.out.println("ThreadB started.");
synchronized (lock) {
System.out.println("ThreadB get the lock.");
try {
sleep(3000);//拿到锁之后3秒唤醒线程A
} catch (InterruptedException e) {
e.printStackTrace();
}
lock.notify();
}
System.out.println("ThreadB release the lock.");
System.out.println("ThreadB end.");
}
}
运行结果:
ThreadA started.
ThreadA get the lock.
ThreadB started.
ThreadB get the lock.
Thread A:WAITING,Thread B:TIMED_WAITING
Thread A:WAITING,Thread B:TIMED_WAITING
Thread A:WAITING,Thread B:TIMED_WAITING
Thread A:WAITING,Thread B:TIMED_WAITING
Thread A:WAITING,Thread B:TIMED_WAITING
Thread A:WAITING,Thread B:TIMED_WAITING
ThreadB release the lock.
ThreadB end.
ThreadA release the lock.
ThreadA end.
Process finished with exit code 0
1)首先,线程A先启动,获得lock
2)随后线程A马上调用 wait,释放锁并进入 WAITING 状态
3)线程B获得锁,sleep 3 秒,由于sleep是有时间的,所以B进入TIMED_WAITING状态
4)线程B调用 notify 唤醒线程A,自己运行结束
5)线程A再次获得lock并运行结束
2)设置超时时间
其他部分和上个例子一样,只是线程A的 wait 方法设置成最大等待 2 秒
class ThreadA2 extends Thread {
private Object lock;
public ThreadA2(Object lock) {
this.lock = lock;
}
@Override
public void run() {
System.out.println("ThreadA started.");
synchronized (lock) {
System.out.println("ThreadA get the lock.");
try {
lock.wait(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("ThreadA release the lock.");
System.out.println("ThreadA end.");
}
}
运行结果:
ThreadA started.
ThreadA get the lock.
ThreadB started.
ThreadB get the lock.
ThreadA:TIMED_WAITING,Thread B:TIMED_WAITING
ThreadA:TIMED_WAITING,Thread B:TIMED_WAITING
ThreadA:BLOCKED,Thread B:TIMED_WAITING
ThreadA:BLOCKED,Thread B:TIMED_WAITING
ThreadA:BLOCKED,Thread B:TIMED_WAITING
ThreadA:BLOCKED,Thread B:TIMED_WAITING
ThreadB release the lock.
ThreadB end.
ThreadA release the lock.
ThreadA end.
Process finished with exit code 0
1)线程A先启动,随后调用 wait(2000) 方法进入TIMED_WAITING 状态并释放锁
2)线程B获得锁并调用 sleep 方法,同样进入TIMED_WAITING状态
3)线程A wait 方法超时后尝试重新获得锁,但因为锁被线程B占用,因此线程A进入BLOCKED状态
4)线程B sleep 结束,释放锁并运行结束
5)线程A再次获得锁并运行结束
-WAITING 和 BLOCKED
BLOCKED 与 WAITING 的区别在于BLOCKED是等待锁时的状态
示例:
class ThreadA3 extends Thread {
private Object lock;
public ThreadA3(Object lock) {
this.lock = lock;
}
@Override
public void run() {
System.out.println("ThreadA started.");
synchronized (lock) {
System.out.println("ThreadA get the lock.");
try {
sleep(3000);
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("ThreadA release the lock.");
System.out.println("ThreadA end.");
}
}
class ThreadB3 extends Thread {
private Object lock;
public ThreadB3(Object lock) {
this.lock = lock;
}
@Override
public void run() {
System.out.println("ThreadB started.");
synchronized (lock) {
System.out.println("ThreadB get the lock.");
lock.notify();
try {
sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("ThreadB release the lock.");
System.out.println("ThreadB end.");
}
}
运行结果:
ThreadA started.
ThreadA get the lock.
ThreadB started.
Thread A:TIMED_WAITING,Thread B:BLOCKED
Thread A:TIMED_WAITING,Thread B:BLOCKED
Thread A:TIMED_WAITING,Thread B:BLOCKED
Thread A:TIMED_WAITING,Thread B:BLOCKED
ThreadB get the lock.
Thread A:BLOCKED,Thread B:TIMED_WAITING
Thread A:BLOCKED,Thread B:TIMED_WAITING
Thread A:BLOCKED,Thread B:TIMED_WAITING
Thread A:BLOCKED,Thread B:TIMED_WAITING
Thread A:BLOCKED,Thread B:TIMED_WAITING
Thread A:BLOCKED,Thread B:TIMED_WAITING
ThreadB release the lock.
ThreadB end.
ThreadA release the lock.
ThreadA end.
1)线程A先启动并获得锁,调用sleep 进入TIMED_WAITING状态
2)线程B竞争锁失败,进入 BLOCKED 状态
3)线程A调用wait方法释放锁并等待唤醒
4)线程B获得锁并唤醒线程A,自己进入TIMED_WAITING状态
5)线程A被唤醒后尝试重新获取锁但失败,进入BLOCKED状态
6)线程B结束并释放锁
7)线程A获得锁并结束线程
总结
- 新建未start的线程状态为NEW
- 调用wait、join并且没设置超时的线程状态为WAITING
- 调用wait、join、sleep并且设置超时的线程状态为TIMED_WAITING
- 等待锁的线程状态为BLOCKED
- 其他运行中的线程状态为RUNNABLE
- 运行结束的线程状态为TERMINATED