Java线程有多少个状态?
直接查看Thread.state类
public enum State {
NEW,// 尚未启动的线程处于此状态
RUNNABLE,// 在Java虚拟机中执行的线程处于此状态
BLOCKED,//被堵塞等待监视器锁定的线程处于此状态
WAITING,//等待,死死的等,等待另一个线程执行特定动作的线程处于此状态
TIMED_WAITING,//超时等待,等待另一个线程执行动作达到指定等待时间的线程处于此状态。
TERMINATED;//线程终止,已退出的线程处于此状态
}
/**
* 测试 NEW RUNNABLE TIMED_WAITING TERMINATED
* @author LCW
* @since 2020/11/18 21:26
**/
public class Test01 {
public static void main(String[] args) throws InterruptedException {
//启动的时候
Thread thread = new Thread(() -> {
//开始执行start(),线程显示RUNNABLE
System.err.println(Thread.currentThread().getState());
//执行sleep方法会使得线程进入TIMED_WAITING
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println(Thread.currentThread().getState());
}, "A");
System.out.println("A==>" +thread.getState());//还没执行start方法,thread的状态就是NEW
thread.start();//开始执行start(),线程显示RUNNABLE
int i = 3;
while (i-->0) {
System.out.println("A==>" + thread.getState());
TimeUnit.SECONDS.sleep(1);//线程结束TERMINATED
}
}
}
/**
* 测试WAITING,该线程执行了wait()方法就会进入WAITING状态
* @author LCW
* @since 2020/11/18 21:26
**/
public class Test02 {
public static void main(String[] args) throws InterruptedException {
//开启两个线程
MyRunnable myRunnable = new MyRunnable();
Thread threadA = new Thread(myRunnable, "A");
Thread threadB = new Thread(myRunnable, "B");
//线程A执行,进入WAITING状态
threadA.start();
int i = 3;
while (i-- > 0) {//打印查看线程A的状态
System.out.println("A==>" + threadA.getState());
System.out.println("B==>" + threadB.getState());
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
threadB.start();
TimeUnit.SECONDS.sleep(1);
System.out.println("A==>" + threadA.getState());
System.out.println("B==>" + threadB.getState());
}
}
class MyRunnable implements Runnable {
@Override
public void run() {
if (Thread.currentThread().getName().equals("A")) {
testA();
} else {
testB();
}
}
private synchronized void testB() {//这个只是为了最后释放线程
this.notifyAll();
}
public synchronized void testA() {//将该线程进程堵塞,此时线程A的State就是WAITING
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 测试BLOCKED
* @author LCW
* @since 2020/11/18 22:40
**/
public class Test03 {
public static void main(String[] args) {
MyRunnable2 myRunnable2 = new MyRunnable2();
//当线程A,B需要获取同一把锁的时候
Thread threadA = new Thread(myRunnable2, "A");
Thread threadB = new Thread(myRunnable2, "B");
threadA.start();
threadB.start();
int i = 3;
while (i-- > 0) {//打印查看线程A的状态
System.out.println("A==>" + threadA.getState());
System.out.println("B==>" + threadB.getState());
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class MyRunnable2 implements Runnable {
@Override
public void run() {
test();
}
private synchronized void test() {
while (true) {
}
}
}
总结:
- NEW就是在线程还没有启动的时候的状态
- RUNNABLE 就是线程在执行start()方法后就进入的状态,下面的几种状态都是从RUNNABLE 变换过去的,也就是new第一,RUNNABLE 一定是第二个状态,其次才是下面的几种状态
- WAITING 就是当此线程执行了wait()方法的时候,线程进入的状态,死死的等待,直到其他线程执行了notify()或者notifyAll()方法才继续执行。
注意:当线程执行wait()后,会释放锁,这跟sleep()不同,sleep()是不释放锁的。 - TIMED_WAITING 就是当线程执行了sleep()方法或者TimeUtil的sleep()方法,线程进入的状态
- BLOCKED就是当两个线程同时需要获取同一把锁,一个线程拿到了锁,那么另一个线程就进入了BLOCKED状态,等待拿到锁的线程执行完毕,才继续执行。
- TERMINATED就是线程结束后的状态