Java线程的生命周期
Posted 热心邻居老王
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java线程的生命周期相关的知识,希望对你有一定的参考价值。
java.lang.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;
}
1.生命周期
一个事物从出生的那一刻开始到最终死亡中间的整个过程.在事物的漫长的生命周期过程中,总会经历不同的状态(婴儿状态/青少年状态/中年状态/老年状态...).线程也是有生命周期的,也是存在不同的状态的,状态相互之间的转换.
2.根据java.lang.Thread.State线程的几种状态可以大致描绘出java线程的生命周期
-
NEW 新建
线程刚被创建,还没调用start方法,或者刚刚调用了start方法,调用start方法不一定"立即"改变线程状态,中间可能需要一些步骤才完成一个线程的启动。 -
RUNNABLE 可行性
start方法调用结束,线程由NEW变成RUNNABLE,线程存活着,并尝试抢占CPU资源,或者已经抢占到CPU资源正在运行,这俩种情况的状态都显示为RUNNABLE分别为:
1)就绪:等待分配时间片
2)运行:抢到CPU资源(分配到时间片)后执行代码 -
BLOCKED 锁阻塞
线程A和线程B都要执行方法test,而且方法test被加了锁,线程A先拿到了锁去执行test方法,线程B这时候需要等待线程A把锁释放。这时候线程B就是处理BLOCKED(锁池) -
WAITING 无限期等待
一个线程在等待另一个线程执行一个(唤醒)动作时,该线程进入Waiting状态。进入这个状态后是不能自动唤醒的,必须等待另一个线程调用notify或者notifyAll方法才能够唤醒。还有join(无参)也会让当前线程进入WAITING状态 -
TIMED_WAITING 有限期等待
和WAITING状态类似,但是有一个时间期限,时间到了,自己也会主动醒来(sleep、join(有参)) -
TERMINATED 终止(死亡)
run方法执行结束的线程处于这种状态
作图如下:
蓝色字体部分表示线程当前状态
需要注意的是:在一个同步代码中,释放锁对象(锁对象.wait())该线程进入wait等待区后是无法再对锁对象进行操作的!!!所以notify()、notifyAll()要写在锁对象释放之前,例如锁对象调用notifyAll()后wait等待区中所有需要该锁对象的线程被唤醒进入锁池等待该锁对象,然后才开始释放锁对象,JVM将随机从锁池中挑选一个需要该锁对象的线程进入就绪状态
以上是关于Java线程的生命周期的主要内容,如果未能解决你的问题,请参考以下文章