这里从javaAPI层面来描述,根据ThreadState枚举,分为六种状态
NEW线程被创建但是还没有调用start方法
RUNNABLE当调用了start方法之后,注意,javaAPI层面的RUNNABLE涵盖了操作系统层面的就绪状态,运行状态,和由于BIO(阻塞IO)导致的阻塞状态。
TERMINATED线程运行结束
BLOCKED TIMED_WAITING WAITING都是javaAPI层面对阻塞状态的细分,会在后面状态转换一节详述
状态练习
package hello;
import java.util.*;
public class Main{
public static void main(String[] args){
Thread t1=new Thread(()->{
System.out.println("测试new");
},"t1");
Thread t2=new Thread(()->{
while(true) {
}
},"t2");
t2.start();
Thread t3=new Thread(()->{
System.out.println("测试terminated");
},"t3");
t3.start();
Thread t4=new Thread(()->{
synchronized(Main.class){
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
t4.start();
Thread t5=new Thread(()->{
try {
t2.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
t5.start();
Thread t6=new Thread(()->{
synchronized(Main.class) {
System.out.println("你们永远不知道我写的是什么");
}
});
t6.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("t1"+t1.getState());
System.out.println("t2"+t2.getState());
System.out.println("t3"+t3.getState());
System.out.println("t4"+t4.getState());
System.out.println("t5"+t5.getState());
System.out.println("t6"+t6.getState());
}
}
测试terminated
t1NEW
t2RUNNABLE
t3TERMINATED
t4TIMED_WAITING
t5WAITING
t6BLOCKED