Java多线程:4.线程的生命周期-6个状态

135 阅读2分钟

一、线程的6个状态

  • New(新创建)
    使用new Thread()新创建但还没启动的线程。
  • Runnable(可运行和运行中)
    调用start()方法后线程即处于Runnable()状态,无论线程是够真正获取到了资源真正的运行。
    即使调用了start()方法后因为系统没有资源,线程状态依然是runnable。
  • Blocked(被阻塞)
    当线程运行到synchronized代码块时,并且该锁被其他线程拿走了,这个线程的状态就是blocked。
  • Waiting(等待)
    调用了Object.wait()、Thread.join()、LockSupport.park()方法后,线程进入wait状态,可提前唤醒。
  • Timed Waiting(计时等待)
    调用了Thread.sleep(time)、Object.wait(time)、Thread.join(time)、LockSupport.parkNanos(time)、LockSupport.parkUntil(time)方法后,线程进入timed waiting状态。
    可以看做有等待时间期限的wait状态,可提前唤醒。
  • Terminated(终止)
    run()方法执行结束了,或者抛出异常导致run()方法终止,线程进入terminated状态。
    在这里插入图片描述

二、线程的状态演示

1.New、Runnable、Terminated
/**
 * 展示线程的New、Runnable、Terminated状态
 */
public class ThreadStatus {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new DoWork());
        System.out.println("执行new Thread()后子线程的状态:"+thread.getState());

        thread.start();
        System.out.println("执行Thread.start()后子线程的状态:"+thread.getState());

        Thread.sleep(1);
        System.out.println("执行run()时子线程的状态:"+thread.getState());

        Thread.sleep(100);
        System.out.println("执行完run()时子线程的状态:"+thread.getState());
    }
}

class DoWork implements Runnable{
    @Override
    public void run() {
        for (int i=0; i<10000; i++){
            if(i%100==0){
                System.out.println(i);
            }
        }
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.Blocked、TimedWaiting
/**
 * 展示线程的Blocked、TimedWaiting状态
 */
public class ThreadStatus {
    public static void main(String[] args) throws InterruptedException {
        DoWork doWork = new DoWork();
        Thread thread1 = new Thread(doWork);
        Thread thread2 = new Thread(doWork);
        thread1.start();
        thread2.start();

        Thread.sleep(1000);

        System.out.println("thread1进入syn(),等待3s,状态:"+thread1.getState());
        System.out.println("thread2没有syn()的锁,阻塞,状态:"+thread2.getState());
    }
}

class DoWork implements Runnable{
    @Override
    public void run() {
        syn();
    }

    // 只能有一个线程进入,其余想进入的线程阻塞
    private synchronized void syn(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述

3.Waiting
/**
 * 展示线程的Waiting状态
 */
public class ThreadStatus {
    public static void main(String[] args) throws InterruptedException {
        DoWork doWork = new DoWork();
        Thread thread1 = new Thread(doWork);
        thread1.start();

        Thread.sleep(3100);
        System.out.println("thread1执行了wait(),状态:"+thread1.getState());
    }
}

class DoWork implements Runnable{
    @Override
    public void run() {
        syn();
    }

    // 只能有一个线程进入,其余想进入的线程阻塞
    private synchronized void syn(){
        try {
            Thread.sleep(3000);
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述


三、阻塞状态

一般情况下,把Blocked(阻塞)、Waiting(等待)、TimedWaiting(计时等待)都称为阻塞状态。