线程的状态(通过代码案例简单而深入的理解线程的状态)

文章目录

    • 线程的生命周期/状态state
      • 线程状态变化流程图
      • 线程状态
        • NEW
          • 案例举例
          • 控台输出
        • RUNNABLE和RUNNING
          • 案例举例
          • 控台输出
          • 备注说明
        • TIMED_WAITING
          • 案例举例
          • 控台输出
          • 备注说明
        • WAITING
          • 案例举例
          • 控台输出
          • 备注说明
        • Blocked
          • 案例举例
          • 控台说明
        • TERMINATED

线程的生命周期/状态state

线程状态变化流程图

线程的状态(通过代码案例简单而深入的理解线程的状态)_第1张图片

线程状态

NEW

案例举例
package com.gaoxinfu.demo.jdk.rt.java.lang.thread.status;

/**
 * @Description:
 * @Author: gaoxinfu
 * @Date: 2020-06-11 11:48
 */
public class NewStatus {

    public static void main(String[] args) {
        Thread thread=new Thread("thread-a");
        System.out.println("当前线程状态 = "+thread.getState());
    }
}
控台输出
当前线程状态 = NEW

Process finished with exit code 0

RUNNABLE和RUNNING

案例举例

创建一个带有线程处理逻辑的线程类StatusThreadDemo

package com.gaoxinfu.demo.jdk.rt.java.lang.thread.status;

import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author: gaoxinfu
 * @Date: 2020-06-10 16:37
 */
public class StartStatus {

    public static void main(String[] args) {
        StatusThreadDemo thread=new StatusThreadDemo();
        System.out.println("start前线程状态 = "+thread.getState());
        thread.start();
        System.out.println("start后线程状态 = "+thread.getState());
        try {
            thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("start后线程正在运行的状态 = "+thread.getState());


    }
}

class StatusThreadDemo extends Thread{
    @Override
    public void run() {
        for (int i=0;i<5;i++){
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("StatusThreadDemo i="+i);
        }
    }
}


控台输出
start前线程状态 = NEW
start后线程状态 = RUNNABLE
StatusThreadDemo i=0
StatusThreadDemo i=1
StatusThreadDemo i=2
StatusThreadDemo i=3
StatusThreadDemo i=4

Process finished with exit code 0
备注说明
1.这里注意下,start()方法调用成功之后,并不是立刻进行运行,而是状态由New转为Runnable状态
  而后,由操作系统进行调度转为Running运行状态;
2.这里大家需要注意下:Running状态如果由于某种原因被阻塞(如sleep中)或者由于某些原因,指令无法运行,
  那么状态也会转为Runnable,等待操作系统重新调度;
3.这里的操作系统的切换,实际上就是CPU在线程中的调度切换;
4.我们上面演示的“start后线程状态 =”不一定是RUNNABLE 也有可能刚好碰上代码在sleep 就是Timed Wating

TIMED_WAITING

主要是你在调用下面方法的时候,待了超时时间
join
sleep
wait
LockSupport.parkUnit
案例举例
package com.gaoxinfu.demo.jdk.rt.java.lang.thread.status;

import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author: gaoxinfu
 * @Date: 2020-06-11 15:25
 */
public class TimedWaitingStatus {

    public static void main(String[] args)  {
        Thread thread=new Thread(new TimedWaitingThreadDemo());
        thread.start();
        System.out.println("线程sleep中的当前状态 = "+thread.getState());
    }
}



class TimedWaitingThreadDemo implements Runnable{

    @Override
    public void run() {
        System.out.println("开始sleep(5s)方法的调用");
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("结束sleep(5s)方法的调用");

    }
}
控台输出
Connected to the target VM, address: '127.0.0.1:65084', transport: 'socket'
开始sleep(5s)方法的调用
线程sleep中的当前状态 = TIMED_WAITING
结束sleep(5s)方法的调用
Disconnected from the target VM, address: '127.0.0.1:65084', transport: 'socket'

Process finished with exit code 0
备注说明

WAITING

join
wait
LockSupport.parkUnit
1.上面的方法调用的时候,不带超时时间
案例举例
package com.gaoxinfu.demo.jdk.rt.java.lang.thread.status;

import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author: gaoxinfu
 * @Date: 2020-06-11 15:36
 */
public class WaitingStatus {
    public static void main(String[] args) throws InterruptedException {

        Thread thread1=new Thread(new WaitingThreadDemo(),"thread1");
        thread1.start();
        System.out.println(thread1.getState());
    }
}

class WaitingThreadDemo implements Runnable{
    
    @Override
    public void run() {
        try {
            Thread.currentThread().join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Hello 大家好,我是WaitingThreadDemo ="+Thread.currentThread().getName());
    }
}
控台输出
WAITING

Process finished with exit code 130 (interrupted by signal 2: SIGINT)
备注说明

Blocked

1.Blocked阻塞状态是一种锁阻塞的状态;
  如 synchronized的锁
案例举例
package com.gaoxinfu.demo.jdk.rt.java.lang.thread.status;

/**
 * @Description:
 * @Author: gaoxinfu
 * @Date: 2020-06-11 16:04
 */
public class BlockedStatus {


    public static void main(String[] args) {

        Thread thread1=new Thread(new BlockedThreadDemo(),"thread1");
        Thread thread2=new Thread(new BlockedThreadDemo(),"thread1");
        thread1.start();
        thread2.start();
        System.out.println("thread1线程状态 = " +thread1.getState());
        System.out.println("thread2线程状态 = " +thread2.getState());

    }
}

class BlockedThreadDemo implements Runnable{

    @Override
    public void run() {
        synchronized (this){
            try {
                System.out.println("当前线程名字 = "+Thread.currentThread().getName()+",开始锁定代码执行片段");
                Thread.currentThread().sleep(5000);
                System.out.println("当前线程名字 = "+Thread.currentThread().getName()+",结束锁定代码执行片段");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
控台说明
当前线程名字 = thread1,开始锁定代码执行片段
thread1线程状态 = TIMED_WAITING
thread2线程状态 = BLOCKED
当前线程名字 = thread1,开始锁定代码执行片段
当前线程名字 = thread1,结束锁定代码执行片段
当前线程名字 = thread1,结束锁定代码执行片段

Process finished with exit code 0

TERMINATED

1.线程终止状态,也就是线程任务结束时的一个状态

你可能感兴趣的:(线程,面试,JUC)