线程的生命周期

本文详细介绍了Java中线程的几种基本状态,包括NEW(新建)、RUNNABLE(可运行)、BLOCKED(阻塞)、WAITING(等待)、TIMED_WAITING(超时等待)和TERMINATED(终止),以及状态转换的原理。

1.线程状态介绍

NEW

Thread state for a thread which has not yet started.

新建:已经创建线程,但还没有启动

RUNNABLE

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.

可运行:包括就绪状态和运行状态

BLOCKED

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 Object.wait.

阻塞:等待获取锁

WAITING

A thread in the waiting state is waiting for another thread to perform a particular action.

等待:调用以下方法时处于等待状态

Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following methods:

  • Object.wait with no timeout
  • Thread.join with no timeout
  • LockSupport.park
TIMED_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:

  • Thread.sleep
  • Object.wait with timeout
  • Thread.join with timeout
  • LockSupport.parkNanos
  • LockSupport.parkUntil
TERMINATED

Thread state for a terminated thread. The thread has completed execution.

终止:线程执行完毕。

2.状态之间的转换

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值