【并发编程】线程生命周期状态

前言

Github:https://github.com/yihonglei/jdk-source-code-reading(java-concurrent)

一 线程生命周期状态

1、在 Thread 类中,有个表示线程状态的 State 枚举。

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;
}

2、Java 线程状态扭转流程图

NEW(初始状态):线程被构建,还没有调用 start 方法,线程还没有启动。

RUNNABLE(可运行状态):表示在 jvm 里面该线程可以执行,但是需要等待 CPU 调度,如果获取 CPU 执行片段,

将开始运行,执行 run 方法。

Running(运行中):注意,Java 线程是没有这个状态的,只是为了更好的理解线程状态扭转,把获取 cpu 片段正在执行的线程抽象出这么一个过程状态。

BLOCKED(阻塞状态):线程运行中等待进入锁,就会处于阻塞状态。

WAITING(等待状态):线程进入等待状态,如图中方式,需要通知。

TIMED_WAITING(等待超时状态):线程处于等待状态,超过等待时长自动恢复。

TERMINATED(终止状态):执行 Stop 方法或者线程执行 run 方法完成或抛异常中断。

二 线程状态实例分析

SleepUtil,等待工具类。

package com.jpeony.concurrent.util;

import java.util.concurrent.TimeUnit;

/**
 * 休眠工具
 *
 * @author yihonglei
 */
public class SleepUtil {
    public static void second(long seconds) {
        try {
            TimeUnit.SECONDS.sleep(seconds);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

ThreadState,线程状态演示类。

package com.jpeony.concurrent.thread;

import com.jpeony.concurrent.util.SleepUtil;

/**
 * 线程生命周期
 *
 * @author yihonglei
 */
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();
    }

    /**
     * 该线程不断地进行睡眠
     */
    private static class TimeWaiting implements Runnable {
        @Override
        public void run() {
            while (true) {
                SleepUtil.second(100);
            }
        }
    }

    /**
     * 该线程在Waiting.class实例上等待
     */
    private 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实例上加锁后,不会释放该锁
     */
    private static class Blocked implements Runnable {
        @Override
        public void run() {
            synchronized (Blocked.class) {
                while (true) {
                    SleepUtil.second(100);
                }
            }
        }
    }
}

运行程序,查看线程堆栈。

1、jps,找到ThreadState进程。

2、jstack加进程id查看堆栈。

详细堆栈如下:

1、BlockedThread-2、BlockedThread-1

BlockedThread-2和BlockedThread-1启动时都去获取同步锁,谁先抢到,就是谁的。

从堆栈可以看出,BlockedThread-1先抢到锁,进入方法后执行While(true),线程一直等待执行,

所以出现超时,线程状态为TIMED_WAITING(等待超时状态)。

因为BlockedThread-1一直没有退出,一直持有锁,当BlockedThread-2进入的时候,获取不到锁,被阻塞了,

线程状态为BLOCKED(阻塞状态)。

2、WaitingThread

线程调用wait方法,处于等待状态WAITING(等待状态),等待被通知。

3、TimeWaitingThread

线程一直等待,等待超时(TIME_WAITING)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值