✨✨hello,愿意点进来的小伙伴们,你们好呐!
🐻🐻系列专栏:【JavaEE初阶】
🐲🐲本篇内容:线程状态详解
🐯🐯作者简介:一名现大二的三非编程小白,日复一日,仍需努力。
Java线程的运行的生命周期可以分为6种不同的状态,在某一时刻线程只能处于其中一种状态

NEW : 初始状态,表示线程任务被构建,但是还没有调用start()方法.
RUNNABLE : 运行状态,在这个状态中,线程正在执行中或者线程在就绪队列中等待CPU的调度都为这个状态.
BLOCKED : 阻塞状态,表示线程阻塞干预.
WAITING : 等待状态,表示线程当前正在等待其他线程,比如调用 join(),wait()方法都会导致线程进入该状态
TIME_WAITING : 超时等待状态,这个状态不同意WAITING,这个状态也是等待,但是它会等待指定的时间后自己返回.
TERMINATED : 终止状态,表示当前线程已经执行完毕,即PCB释放了,但是线程对象的引用还在.
下面的图解会让我们更清晰的了解到线程创建,执行,等待,通知,销毁的一系列流程中的状态
 
💦💦💦接下来我来带大家体会线程的状态
- NEW:
- RUNNABLE:
- WAITING:
- TIMED_WAITING:
- BLOCKED:
- TERMINATED:
 
NEW:
public class ThreadDemo_NEW {
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            System.out.println("演示NEW");
        });
        System.out.println(thread.getName() + " 状态为: " + thread.getState());
    }
}

在构建线程后没有启动线程前,线程处于NEW状态
RUNNABLE:
💨💨💨因为就绪状态与执行状态在Java中无法体现出来,都归为RUNNABLE状态,所以我就来展示RUNNABLE状态
public class ThreadDemo_RUNNABLE {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            System.out.println("演示RUNNABLE");
        });
        thread.start();
        System.out.println(thread.getName() + " 状态为: " + thread.getState());
        Thread.sleep(1000);
    }
}

在此时,线程已经启动所以状态为RUNNABLE
WAITING:
public class ThreadDemo_WAITING {
    public static void main(String[] args) throws InterruptedException {
        Object o = new Object();
        Thread thread = new Thread(() -> {
            System.out.println("演示WAITING");
            synchronized (o){
                try {
                    o.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
        Thread.sleep(1000);
        System.out.println(thread.getName() + " 状态为: " + thread.getState());
    }
}


我们使用wait() 方法让Thread-0线程进入等待状态,这个时候没有指定等待时间,所以是死等
TIMED_WAITING:
public class ThreadDemo_TIMED_WAITING {
    public static void main(String[] args) throws InterruptedException {
        Object o = new Object();
        Thread thread = new Thread(() -> {
            System.out.println("演示TIMED_WAITING");
            synchronized (o){
                try {
                    o.wait(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
        Thread.sleep(1000);
        System.out.println(thread.getName() + " 状态为: " + thread.getState());
    }
}

当指定等待时间后,线程就进入了超时等待的状态
BLOCKED:
public class ThreadDemo_BLOCKED {
    public static void main(String[] args) throws Exception {
        testBlocked();
    }
    public static void testBlocked() throws Exception {
        class Counter {
            int counter;
            public synchronized void increase() {
                counter++;
                try {
                    Thread.sleep(30000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        Counter c = new Counter();
        Thread t1 = new Thread(new Runnable() {
            public void run() {
                c.increase();
            }
        }, "t1线程");
        t1.start();
        Thread t2 = new Thread(new Runnable() {
            public void run() {
                c.increase();
            }
        }, "t2线程");
        t2.start();
        Thread.sleep(100); // 确保 t2 run已经得到执行
        System.out.println(t2.getName() + " 状态为: " + t2.getState());
    }
}
线程1 与线程2 都要争夺执行increase() 方法,最终导致的线程2阻塞
TERMINATED:
public class ThreadDemo_TERMINATED {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            System.out.println("演示TERMINATED");
        });
        thread.start();
        Thread.sleep(1000);
        System.out.println(thread.getName() + " 状态为: " + thread.getState());
    }
}
线程执行结束后,但是线程引用还有指向,未销毁,所以这个时候线程的状态就是终止状态
通过上面的展示,我们可以了解到线程自身的生命周期并不是固定的,而是通过代码执行在不同的状态下进行切换,对于线程的状态我们要很清楚的把握住.



















