09 Java多线程的状态详解 NEW, RUNNABLE, BLOCKED, WAITING, TIME_WAITING, TERMINATED,jstack查看dump文件中线程的状态

Java多线程的状态

    • 线程的状态
    • 多线程状态示例
    • jstack 查看dump文件,通过dump文件查看线程的状态

线程的状态

状态 描述
NEW 初始状态,线程被构建,但未调用start()方法。
RUNNABLE 运行状态,Java线程将操作系统中的就绪(Ready)状态和运行(Running)状态合并起来称为运行状态。就绪(Ready)状态是将线程扔到CPU的等待队列里面去排队等待执行;当获得到CPU并开始执行线程的时候才叫(Running)状态。
BLOCKED 阻塞状态,在执行同步代码块,未获得锁时就会阻塞。
WAITING 等待状态,表示线程进入等待状态,需要等待其它线程通知(唤醒)或中断
TIME_WAITING 超时等待状态,指定时间后自动唤醒自己。
TERMINATED 终止状态,代表线程执行完成
// Thread类中有个State枚举类型
public enum State {
    NEW,
    RUNNABLE,
    BLOCKED,
    WAITING,
    TIMED_WAITING,
    TERMINATED;
}

09 Java多线程的状态详解 NEW, RUNNABLE, BLOCKED, WAITING, TIME_WAITING, TERMINATED,jstack查看dump文件中线程的状态_第1张图片

NEW 新建状态

当new一个线程时,还未调用start()方法之前,线程处于新建状态

RUNNABLE 运行状态

线程对象调用start()方法时候,线程会被线程调度器来执行,也就是交给操作系统来执行了,操作系统来执行线程的整个过程;处于这个过程之间的线程状态为运行状态

Runnable内部有两个状态:

  • Ready 就绪态态:线程在CPU的等待队列里面排队等待CPU运行
  • Running 运行状态:线程在CPU上去运行的时候才叫Running运行状态。

调用yiled时候会从running状态跑到Ready状态去;当CPU执行线程时,线程从Ready状态跑到Running状态。

BLOCKED 阻塞状态

同步代码块的情况下没有得到锁就会被阻塞状态

获得锁的时候就是就绪状态

在运行的时候如果调用了wait() jion() LockSupport.park()进入Waiting状态

调用notify() notifiAll() LockSupport.park()就又回到Running状态

TimedWaiting按照时间等待,等待时间结束线程自己就会去了,Thread.sleep(time) wait(time) jion(time) LockSupport.parkNanos() LockSupport.parkUntil()

WAITING 等待状态

Runnable状态里面的另一种状态

TIMED_WAITING 等待状态

Runnable状态里面的另一种状态

TERMINATED 终止状态

线程顺利执行完毕,进入terminated终止状态。

多线程状态示例

import java.util.concurrent.TimeUnit;

/**
 * 线程状态示例
 */
public class ThreadState {
    public static void main(String[] args) {
        new Thread(new TimeWaiting(), "TimeWaitingThread").start();
        new Thread(new Waiting(), "WaitingThread").start();
        // 使用两个Blocked线程,一个获取锁成功,另一个被阻塞
        new Thread(new Blocked(), "BlockedThread-1").start();
        new Thread(new Blocked(), "BlockedThread-2").start();
    }

    // 该线程不断地进行睡眠
    static class TimeWaiting implements Runnable {
        @Override
        public void run() {
            while (true) {
                try {
                    TimeUnit.SECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 该线程在Waiting.class实例上等待
    static class Waiting implements Runnable {
        @Override
        public void run() {
            while (true) {
                synchronized (Waiting.class) {
                    try {
                        Waiting.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    // 该线程在Blocked.class实例上加锁后,不会释放该锁
    static class Blocked implements Runnable {
        public void run() {
            synchronized (Blocked.class) {
                while (true) {
                    try {
                        TimeUnit.SECONDS.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

jstack 查看dump文件,通过dump文件查看线程的状态

//查看状态
......>jps
25584 Launcher
11892
13252 ThreadState
7284 Jps

......>jstack 13252
2023-04-06 19:45:22
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.271-b09 mixed mode):

// 因Blocked.class被"BlockedThread-1"加锁,所以"BlockedThread-2"为阻塞状态
"BlockedThread-2" #15 prio=5 os_prio=0 tid=0x000001eecd892000 nid=0x62e8 waiting for monitor entry [0x000000c6b75ff000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at com.thread.ThreadState$Blocked.run(ThreadState.java:53)
        - waiting to lock <0x0000000715c5f968> (a java.lang.Class for com.thread.ThreadState$Blocked)
        at java.lang.Thread.run(Thread.java:748)
//执行sleep()方法后,线程由RUNNABLE变为TIMED_WAITING
"BlockedThread-1" #14 prio=5 os_prio=0 tid=0x000001eecd897800 nid=0x5ac4 waiting on condition [0x000000c6b74ff000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
        at java.lang.Thread.sleep(Thread.java:340)
        at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
        at com.thread.ThreadState$Blocked.run(ThreadState.java:53)
        - locked <0x0000000715c5f968> (a java.lang.Class for com.thread.ThreadState$Blocked)
        at java.lang.Thread.run(Thread.java:748)

//执行wait()方法后,线程由RUNNABLE变为WAITING
"WaitingThread" #13 prio=5 os_prio=0 tid=0x000001eecd898000 nid=0x5e80 in Object.wait() [0x000000c6b73fe000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x0000000715c5d168> (a java.lang.Class for com.thread.ThreadState$Waiting)
        at java.lang.Object.wait(Object.java:502)
        at com.thread.ThreadState$Waiting.run(ThreadState.java:38)
        - locked <0x0000000715c5d168> (a java.lang.Class for com.thread.ThreadState$Waiting)
        at java.lang.Thread.run(Thread.java:748)
       
//执行sleep()方法后,线程由RUNNABLE变为TIMED_WAITING
"TimeWaitingThread" #12 prio=5 os_prio=0 tid=0x000001eecd894800 nid=0x5098 waiting on condition [0x000000c6b72ff000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
        at java.lang.Thread.sleep(Thread.java:340)
        at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
        at com.thread.ThreadState$TimeWaiting.run(ThreadState.java:23)
        at java.lang.Thread.run(Thread.java:748)

你可能感兴趣的:(Java,Thread,ThreadPool,Java多线程的状态详解,jstack查看dump文件,Java多线程,java)