美文网首页
代码模拟线程的6种状态

代码模拟线程的6种状态

作者: lconcise | 来源:发表于2022-03-03 00:23 被阅读0次

线程6种状态:

  • NEW

Thread state for a thread which has not yet started.

  • RUNNABLE

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.

  • BLOCKED

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

  • WAITING

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

A thread in the waiting state is waiting for another thread to perform a particular action. For example, a thread that has called Object.wait() on an object is waiting for another thread to call Object.notify() or Object.notifyAll() on that object. A thread that has called Thread.join() is waiting for a specified thread to terminate.

  • TIME_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.

代码模拟线程的6种状态

public class ThreadState {

    public static void main(String[] args) throws Exception {
        Thread t1 = new Thread(() ->{
            System.out.println("2:"+Thread.currentThread().getState());
            for (int i = 0;i < 3;i++){
                SleepHelp1.sleepseconds(1);
                System.out.print(i+" ");
            }
            System.out.println();
        });
        System.out.println("1:"+t1.getState());
        t1.start();
        t1.join();
        System.out.println("3:"+t1.getState());

        //=====================================================================
        Thread t2 = new Thread(()->{
            //阻塞  等着被叫醒  waiting
            LockSupport.park();
            System.out.println("t2 go on");
            SleepHelp1.sleepseconds(5);

        });
        t2.start();
        SleepHelp1.sleepseconds(1);
        System.out.println("4:"+t2.getState());

        //叫醒线程
        LockSupport.unpark(t2);
        SleepHelp1.sleepseconds(1);
        System.out.println("5:"+t2.getState());
        //=====================================================================

        final Object o = new Object();
        Thread t3 = new Thread(()->{
            synchronized (o){
                System.out.println("t3 得到了 锁 o");
            }
        });

        new Thread(()->{
            synchronized (o){
                SleepHelp1.sleepseconds(5);
            }
        }).start();

        SleepHelp1.sleepseconds(1);

        t3.start();

        SleepHelp1.sleepseconds(1);

        System.out.println("6:"+t3.getState());
        //===========================================================
        //juc的锁 盲等待, 不会进入 block 的状态,进入 waiting 状态
        final Lock lock = new ReentrantLock();
        Thread t4 = new Thread(()->{
            lock.lock();//省略掉 try ... finally
            System.out.println("t4 拿到了锁 o");
            lock.unlock();
        });

        new Thread(()->{
            lock.lock();
            SleepHelp1.sleepseconds(5);
            lock.unlock();
        }).start();

        SleepHelp1.sleepseconds(1);

        t4.start();

        SleepHelp1.sleepseconds(1);

        System.out.println("7:"+t4.getState());
        //=======================================================
        Thread t5 = new Thread(()->{
            LockSupport.park();
        });

        t5.start();

        SleepHelp1.sleepseconds(1);

        System.out.println("8:"+t5.getState());

        SleepHelp1.sleepseconds(1);

        LockSupport.unpark(t5);
    }

}

class SleepHelp1 {

    public static void sleepseconds(int seconds){
        try {
            TimeUnit.SECONDS.sleep(seconds);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}

执行结果:

1:NEW
2:RUNNABLE
0 1 2 
3:TERMINATED
4:WAITING
t2 go on
5:TIMED_WAITING
6:BLOCKED
7:WAITING
t3 得到了 锁 o
8:WAITING
t4 拿到了锁 o

相关文章

  • 代码模拟线程的6种状态

    线程6种状态: NEW Thread state for a thread which has not yet s...

  • 线程安全----原子类

    首先我们来看一段代码(模拟抢货): 代码解释:下面代码模拟并发抢货过程,我们用了6个线程处理6万的货,按理来说应该...

  • 线程状态

    线程状态 新建状态new出一个线程,线程还没有开始运行,当线程处于新建状态时,程序还没有运行线程中的代码。 就绪状...

  • 线程状态和线程终止

    一. 线程状态 线程的6种状态 线程运行状态图 代码示例 1.第一种:新建 -> 运行 -> 终止 运行结果: 2...

  • C#多线程

    C#多线程 一多线程的几种方式 统一用于测试的模拟下载代码 1:委托创建线程实现异步 实例1:Action委托创建...

  • 面试总结

    2、什么情况下会让线程进入死亡状态: 1.线程中的代码执行完毕2.执行线程的代码时抛出异常 3、线程的interr...

  • JS任务调度方式

    JS是的执行环境是单线程的,任务的调度方式是排队模拟事件排队代码

  • 死锁的排查方法

    本地模拟死锁环境 idea可以查看线程状态image.png jstack定位image.pngimage.png...

  • 【iOS开发】多线程 - 概述

    线程主线程后台线程 线程是被模拟出来的,CPU通过分配时间片的方法模拟出多线程CPU通过分配时间片的方法模拟出多线...

  • Java并发编程实战读书笔记之线程安全性

    一、术语 有状态(无状态) 线程安全 原子性 竞态条件 要编写线程安全的代码,其核心是要对状态访问操作进行管理,特...

网友评论

      本文标题:代码模拟线程的6种状态

      本文链接:https://www.haomeiwen.com/subject/zxcjrrtx.html