前言
回顾一下
到现在,我们已经知道了
- 线程的三种创建方式及其选择
- 线程的常用方法
- 线程的安全问题以及线程的死锁
- 线程之间的通信(等待唤醒)
今天,我们一起来看看线程的生命周期,生命周期这个词在后续的javaweb 相关知识点中会经常提起
知己知彼,想要用好线程,就得先了解它
注意:面试要是有问多线程的知识点,这一定会问(建议收藏,面试前拿出来抱抱佛脚)
线程的生命周期
我们来看一段源码,打开thread类的源代码,注意是 lang包下的
在thread类里面通过关键字搜索
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;
}
这是 Thread 类里面的一段枚举,这里的六个状态就是 线程的生命周期,,也可以说是五个状态,因为 waiting 跟 timed_waiting 可以统称为阻塞
这也是为什么有的人说是五个状态,细分就是六个状态
1. new (新建),尚未启动的线程的状态
public class Demo implements Runnable {
@Override
public void run() {
}
public static void main(String[] args) {
Demo demo = new Demo();
Thread thread = new Thread(demo);
//当前线程状态
System.out.println(thread.getState());
}
}
就是我们只是创建线程,并没有 调用start() 开启 线程的状态
输出结果
2. runnable (就绪),线程已经启动,正在等CPU分配资源,已经调用start()方法后
public class Demo implements Runnable {
@Override
public void run() {
}
public static void main(String[] args) {
Demo demo = new Demo();
Thread thread = new Thread(demo);
thread.start();
//当前线程状态
System.out.println(thread.getState());
}
}
3.blocked (堵塞) 线程让出自己的执行权
来简单的模拟两个线程在抢占同一个锁
public class Demo implements Runnable{
@Override
public void run() {
getTime();
}
public synchronized void getTime() {
while (true){
}
}
public static void main(String[] args) {
Demo demo =new Demo();
Thread A =new Thread(demo);
Thread B =new Thread(demo);
A.start();
B.start();
int i=5;
while(i-- >0) {
System.out.println("A的状态"+A.getState());
System.out.println("B的状态"+B.getState());
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
可以看到线程进入阻塞状态
3.waiting 线程进入等待状态
让线程进入等待状态你可以使用wait,join ,sleep , park 等等
4.timed_waiting
这个状态与Waiting 状态类似,都是线程处于等待状态,区别是这个状态线程不会进入无限等待状态,比如我们之前使用wait() 让线程进入等待状态,需要notify唤醒,不唤醒他将一直等待下去,然而 当线程TIMED_WAITING 状态,没有完后期望操作,在指定时间内他将会自动转换成Runnable 状态
一般是调用wait(long timeout)
这个单位是毫秒 ,可以看到原码
5. terminated 线程进入终止状态
这个就是线程完成任务,生命周期结束了,执行完run方法,或者调用线程的stop方法,stop方法已经不建议使用,这里提一下,当然出现异常也会使线程终止
为了方便记忆,下面来看一张图解:
我们这边把 waiting 跟 timed_waiting 统一为阻塞状态
以上就是线程的生命周期