• Java中线程的状态


    1.线程的状态

    本文以java1.8为例,存在类java.lang.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:
             * 
      *
    • {@link Object#wait() Object.wait} with no timeout
    • *
    • {@link #join() Thread.join} with no timeout
    • *
    • {@link LockSupport#park() 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. */ 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: *

      *
    • {@link #sleep Thread.sleep}
    • *
    • {@link Object#wait(long) Object.wait} with timeout
    • *
    • {@link #join(long) Thread.join} with timeout
    • *
    • {@link LockSupport#parkNanos LockSupport.parkNanos}
    • *
    • {@link LockSupport#parkUntil LockSupport.parkUntil}
    • *
    */ TIMED_WAITING, /** * Thread state for a terminated thread. * The thread has completed execution. */ TERMINATED; }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    2.程序状态的模拟

    2.1 NEW

    线程没有启动之前的状态.

     @Test
        public void New() throws InterruptedException {
            Thread t1 = new Thread(()->{
                System.out.println("2:"+Thread.currentThread().getState());
            });
            System.out.println("1:"+t1.getState());
            t1.start();
            t1.join();
            System.out.println("3:"+t1.getState());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    输出结果:

    1:NEW
    2:RUNNABLE
    3:TERMINATED
    
    
    • 1
    • 2
    • 3
    • 4

    2.2 RUNNABLE

    等待其他来自操作系统如处理器的状态。
    例子如上。

    2.3 BLOCKED

    线程在等待锁的监控锁的状态,synchronized代码块或者方法。

     public void Bolocked() throws InterruptedException {
            final Object o = new Object();
            Thread t1 = new Thread(()->{
                synchronized (o){
                    try {
                        System.out.println("t1申请到了锁....");
                        TimeUnit.SECONDS.sleep(2);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            new Thread(()->{
                synchronized (o) {
                    try {
                        TimeUnit.SECONDS.sleep(5);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }).start();
            TimeUnit.SECONDS.sleep(1);
            t1.start();
            TimeUnit.SECONDS.sleep(1);
            System.out.println("t1:"+t1.getState());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    输出状态

    t1:BLOCKED
    
    • 1

    2.4 WAITING

    调用以下方法的时候

    • {@link Object#wait() Object.wait} with no timeout
    • {@link #join() Thread.join} with no timeout
    • {@link LockSupport#park() LockSupport.park}
     @Test
        public void Waiting() throws InterruptedException {
            final Lock lock = new ReentrantLock();
            Thread t1 = new Thread(()->{
                lock.lock();
                System.out.println("t1申请到了锁....");
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                lock.unlock();
    
            });
            new Thread(()->{
               lock.lock();
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
               lock.unlock();
            }).start();
            TimeUnit.SECONDS.sleep(1);
            t1.start();
            TimeUnit.SECONDS.sleep(1);
            System.out.println("t1:"+t1.getState());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    输出打印结果:

    t1:WAITING
    
    • 1

    2.5 TIMED_WAITING

    • {@link #sleep Thread.sleep}
    • {@link Object#wait(long) Object.wait} with timeout
    • {@link #join(long) Thread.join} with timeout
    • {@link LockSupport#parkNanos LockSupport.parkNanos}
    • {@link LockSupport#parkUntil LockSupport.parkUntil}
     @Test
        public void TIMED_WAITING() throws InterruptedException {
            Thread t1 = new Thread(() -> {
                LockSupport.park();
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
            t1.start();
            SleepHelper.sleepSeconds(1);
            System.out.println("1: " + t1.getState());
            LockSupport.unpark(t1);
            TimeUnit.SECONDS.sleep(1);
            System.out.println("2: " + t1.getState());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    输出结果

    1: WAITING
    2: TIMED_WAITING
    
    • 1
    • 2
  • 相关阅读:
    3.数学公式-方程分组共享编号
    第二十三章《斗地主游戏》第1节:斗地主项目简介
    C# 发送邮件
    dwg如何转换成pdf,cad快速转pdf
    为什么使用ray_tune对自己改进的yolov8模型调优时
    常见服务知识点罗列--nginx
    理一理 OC/OD 门、开漏输出、推挽输出等一些相关概念
    docker镜像存储在哪里
    【内网穿透】在Ubuntu搭建Web小游戏网站,并将其发布到公网访问
    【LeetCode】每日一题 2023_11_14 阈值距离内邻居最少的城市(Floyd 最短路算法)
  • 原文地址:https://blog.csdn.net/qq_37400096/article/details/128184711