• 【java】4-线程的状态


    1. 线程状态的含义

    线程在不同的时期具有不同的状态,线程状态是线程实例的一个非常重要的属性,我们可以通过状态看到当前线程究竟是在被创建,运行,阻塞还是已经中断,或是处于别的状态,以便更好地对于代码进行调试。

    我们可以在获取线程实例之后调用getState()方法获知线程当前状态,并且将其打印出来:

    System.out.println(Thread.currentThread().getState());
    
    • 1

    总的来说,线程具有一下的一系列状态,这一系列状态贯穿了一个线程的生命周期,从被创建,到执行,到被销毁,线程具有不同的转态

    线程的状态是一个枚举类型 Thread.State

    public class ThreadState {
    	public static void main(String[] args) {
    		for (Thread.State state : Thread.State.values()) {
    			System.out.println(state);
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • NEW:交代了任务但是没有start
    • RUNNABLE:就绪/运行;排在等待队列中也是属于该状态,即可被调度的状态,是否开始调度,则看调度器
    • TIMED_WAITED:给定时间的阻塞
    • BLOCKED:等待锁的阻塞
    • WAITING:未指定时间的阻塞
    • TERMINATED:终止

    在这里插入图片描述
    Thread.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 {@code Object.wait()} * on an object is waiting for another thread to call * {@code Object.notify()} or {@code Object.notifyAll()} on * that object. A thread that has called {@code 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

    2. isAlive()判断线程是否存活

    public class ThreadStateTransfer {
    	public static void main(String[] args) throws InterruptedException {
    		Thread t = new Thread(() -> {
    			for (int i = 0; i < 1000_0000; i++) {
    			}
    		}, "MyThread");
    		System.out.println(t.getName() + ": " + t.getState());;
    		t.start();
    		while (t.isAlive()) {
    			System.out.println(t.getName() + ": " + t.getState());;
    		}
    		System.out.println(t.getName() + ": " + t.getState());;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    2.1 初探大数据
    JavaScript中的回收机制
    mysql内存会持续上涨,每天增加一点,一直到100%
    阿里云效自动构建python自动测试脚本
    IO流的学习1
    Nginx+Tomcat负载均衡、动静分离
    通义千问:一个专门响应人类指令的大模型
    Linux libreoffice安装 word转pdf 中文乱码(缺少字体解决)
    ASP.NET Core - 缓存之分布式缓存
    学习笔记18--自动驾驶智能化指标评测体系(上)
  • 原文地址:https://blog.csdn.net/m0_52640673/article/details/128167736