线程六态
在Thread
类中的枚举State
列举了JAVA线程共有如下6中状态:
状态 | 含义 |
---|---|
NEW | 新创建了一个线程对象,但还没有调用start()方法 |
RUNNABLE | 在Java虚拟机上执行的线程处于这种状态。 |
BLOCKED | 线程阻塞于锁 |
WAITING | 进入该状态的线程需要等待其他线程做出一些特定动作(通知或中断) |
TIMED_WAITING | 该状态不同于WAITING,它可以在指定的时间后自行返回 |
TERMINATED | 表示该线程已经执行完毕 |
注意:Java线程中将就绪(ready)和运行中(runnin)两种状态笼统的称为“运行”。
线程对象创建后,其他线程(比如main线程)调用了该对象的start()方法。该状态的线程位于可运行线程池中,等待被线程调度选中,获取CPU的使用权,此时处于就绪状态(ready)。就绪状态的线程在获得CPU时间片后变为运行中状态(running)。
状态转换图
线程阻塞状态是线程阻塞在进入synchronized
关键字修饰的方法或代码块(获取锁)时的状态,但是阻塞在java.concurrent包中的Lock
接口的线程状态却是等待状态,因为java.concurrent包中的Lock接口对于阻塞的实现均使用了LockSupport
类中的相关方法。
代码演示
public class ThreadStateTest {
public static void main(String[] args) {
Thread thread = new Thread(()->{
System.out.println("Thread run...");
try {
System.out.println(Thread.currentThread().getState());//RUNNABLE
Thread.sleep(2000);//RUNNABLE->TIMED_WAITING
} catch (InterruptedException e) {
e.printStackTrace();
}
});
System.out.println(thread.getState());//NEW
thread.start();
try {
Thread.sleep(1000);
System.out.println(thread.getState());//TIMED_WAITING
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Thread.sleep(2000);//等待thread运行结束
System.out.println(thread.getState());//TERMINATED
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ThreadStateTest2 {
public static void main(String[] args) {
Object lock = new Object();
Thread thread1 = new Thread(()->{
synchronized (lock){
System.out.println("Thread1 enter synchronized ...");
System.out.println("Thread1 State is "+Thread.currentThread().getState());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread thread2 = new Thread(()->{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock){
System.out.println("Thread2 enter synchronized ...");
System.out.println("Thread2 State is "+Thread.currentThread().getState());
}
});
thread1.start();
thread2.start();
try {
Thread.sleep(1000);
System.out.println("Thread2 State is "+thread2.getState());//BLOCKED
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ThreadStateTest {
public static void main(String[] args) {
Object lock = new Object();
Thread thread1 = new Thread(()->{
synchronized (lock){
try {
System.out.println("Before wait,Thread1 State is "+Thread.currentThread().getState());
lock.wait();
System.out.println("After wait,Thread1 State is "+Thread.currentThread().getState());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread thread2 = new Thread(()->{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock){
System.out.println("Before notify, Thread2 State is "+Thread.currentThread().getState());
System.out.println("In Thread2,Thread1 State is "+thread1.getState());//WAITING
lock.notify();
System.out.println("After notify, Thread2 State is "+Thread.currentThread().getState());
}
});
thread1.start();
thread2.start();
}
}
版权属于:带翅膀的猫
本文链接:https://www.chengpengper.cn/archives/89/
转载时须注明出处及本声明