Java线程的那些状态

108 篇文章 0 订阅

Java线程(或者说计算机线程),是有一些状态来表示当前线程运行信息的,可以通过jsatck命令来查看Java进程中线程函数栈信息,其中就包括了Java线程状态。

在分析Java线程状态之前,我们先来看下进程、线程、协程这些概念之间的区别:

  • 进程:运行时程序的抽象,系统资源管理的基本单位;
  • 线程:一个进程可以包含多个线程,CPU调度执行的基本单位,Linux下称线程为轻量级进程;
  • 协程:一种比线程更加轻量级的存在,线程是由操作系统内核负责管理调度的,而协程一般是由用户进程负责调度管理,多个协程之间可使用同一个线程来运行,比如go协程的实现就是这样的。

对于开发小伙伴来讲,了解Java线程状态,有利于加深对线程的理解,有助于解决线程死锁、线程阻塞等问题。

Java Thread类型的State枚举就定义了如下6种线程状态,这些状态之间会进行切换直到线程终止为止,类似于状态机流转。

public enum State {
    /**
     * Thread state for a thread which has not yet started.
     */
    NEW,

    /**
     * Thread state for a runnable thread.  A thread in the runnable
     * state is executing in the Java virtual machine but it may
     * be waiting for other resources from the operating system
     * such as processor.
     */
    RUNNABLE,

    /**
     * Thread state for a thread blocked waiting for a monitor lock.
     * A thread in the blocked state is waiting for a monitor lock
     * to enter a synchronized block/method or
     * reenter a synchronized block/method after calling
     * {@link Object#wait() Object.wait}.
     */
    BLOCKED,

    /**
     * Thread state for a waiting thread.
     * A thread is in the waiting state due to calling one of the
     * following methods:
     * <ul>
     *   <li>{@link Object#wait() Object.wait} with no timeout</li>
     *   <li>{@link #join() Thread.join} with no timeout</li>
     *   <li>{@link LockSupport#park() LockSupport.park}</li>
     * </ul>
     *
     * <p>A thread in the waiting state is waiting for another thread to
     * perform a particular action.
     *
     * For example, a thread that has called <tt>Object.wait()</tt>
     * on an object is waiting for another thread to call
     * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
     * that object. A thread that has called <tt>Thread.join()</tt>
     * is waiting for a specified thread to terminate.
     */
    WAITING,

    /**
     * Thread state for a waiting thread with a specified waiting time.
     * A thread is in the timed waiting state due to calling one of
     * the following methods with a specified positive waiting time:
     * <ul>
     *   <li>{@link #sleep Thread.sleep}</li>
     *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
     *   <li>{@link #join(long) Thread.join} with timeout</li>
     *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
     *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
     * </ul>
     */
    TIMED_WAITING,

    /**
     * Thread state for a terminated thread.
     * The thread has completed execution.
     */
    TERMINATED;
}
复制代码

这些Java线程状态图如下所示:

注意,RUNNABLE状态时的线程不一定就是RUNNING的,有可能CPU还在执行其他线程而没有调度到该线程。

我们知道了Java层面对于线程状态的几种定义,那么Java/JDK底层是基于什么机制来实现线程管理的呢?下面咱们写个示例Demo进行分析:

@SuppressWarnings("all")
public class MainDemo {

    private static Object obj1 = new Object();
    private static Lock lock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {
        new Thread(new Runnable() {
            @Override
            public void run() {
                LockSupport.park();
            }
        }, "t1").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                LockSupport.park(TimeUnit.DAYS.toNanos(1));
            }
        }, "t2").start();

        lock.lock();
        new Thread(new Runnable() {
            @Override
            public void run() {
                lock.lock();
                System.out.println("zzz222");
            }
        }, "t3").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000000000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "t4").start();

        synchronized (obj1) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (obj1) {
                        System.out.println("zzz");
                    }
                }
            }, "t5").start();

            Thread.sleep(1000000000);
        }
    }
}
复制代码

上述代码起了5个子线程,名字分别是t1、t2到t5,其中t1执行LockSupport.park()阻塞,t2执行LockSupport.park(long)阻塞,t3执行lock.lock()阻塞,t4执行Thread.sleep()阻塞,t5执行synchronized(obj)阻塞。

通过jstack命令可以查看Java线程栈,如下所示:

但是它不能看到JDK和系统层面的线程栈,此时可以通过pstack命令来查看,如下所示:

那么Java线程栈怎么和系统层线程栈关联起来呢?这个时候可以通过Java线程栈中nid(十六进制)和系统层线程栈中LWP是否相等关联起来。比如t1线程的线程栈如下所示:

通过对t1-t5线程栈进行分析,这些线程都处于TIMED_WAITING或者WAITING状态,它们的JDK层面都是走到了方法pthread_cond_wait或pthread_cond_timedwait,而这些就是系统层pthread库中方法了,对于学过 Linux C 的同学肯定都不会陌生。感兴趣的同学可以按照上面的命令查看下所有线程的整个线程栈信息。


作者:luoxn28
链接:https://juejin.cn/post/6941571301337202719
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值