【java】4-线程的状态

1. 线程状态的含义

线程在不同的时期具有不同的状态,线程状态是线程实例的一个非常重要的属性,我们可以通过状态看到当前线程究竟是在被创建,运行,阻塞还是已经中断,或是处于别的状态,以便更好地对于代码进行调试。

我们可以在获取线程实例之后调用getState()方法获知线程当前状态,并且将其打印出来:

System.out.println(Thread.currentThread().getState());

总的来说,线程具有一下的一系列状态,这一系列状态贯穿了一个线程的生命周期,从被创建,到执行,到被销毁,线程具有不同的转态

线程的状态是一个枚举类型 Thread.State

public class ThreadState {
	public static void main(String[] args) {
		for (Thread.State state : Thread.State.values()) {
			System.out.println(state);
		}
	}
}
  • NEW:交代了任务但是没有start
  • RUNNABLE:就绪/运行;排在等待队列中也是属于该状态,即可被调度的状态,是否开始调度,则看调度器
  • TIMED_WAITED:给定时间的阻塞
  • BLOCKED:等待锁的阻塞
  • WAITING:未指定时间的阻塞
  • TERMINATED:终止

在这里插入图片描述
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 {@code Object.wait()}
         * on an object is waiting for another thread to call
         * {@code Object.notify()} or {@code Object.notifyAll()} on
         * that object. A thread that has called {@code Thread.join()}
         * 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. isAlive()判断线程是否存活

public class ThreadStateTransfer {
	public static void main(String[] args) throws InterruptedException {
		Thread t = new Thread(() -> {
			for (int i = 0; i < 1000_0000; i++) {
			}
		}, "MyThread");
		System.out.println(t.getName() + ": " + t.getState());;
		t.start();
		while (t.isAlive()) {
			System.out.println(t.getName() + ": " + t.getState());;
		}
		System.out.println(t.getName() + ": " + t.getState());;
	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DanteIoVeYou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值