zoukankan      html  css  js  c++  java
  • Java并发编程--线程的生命周期

    在 Java 中线程的生命周期中一共有 6 种状态。

    1. NEW

    Thread state for a thread which has not yet started.
    新建状态,线程被创建出来,但尚未启动时的线程状态。

    2. RUNNABLE

    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.
    就绪状态,表示可以运行的线程状态,它可能正在运行,或者是在排队等待操作系统给它分配 CPU 资源。

    3. 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()}.

    4. WAITING

    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.

    5. TIMED_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(long)
    • Object#wait(long) with timeout
    • Thread#join(long) with timeout
    • LockSupport#parkNanos
    • LockSupport#parkUntil

    6. TERMINATED

    The thread has completed execution.

    6种状态的流转如下图:

  • 相关阅读:
    java如何手动创建一个线程池
    HashMap的面试总结(摘抄)
    JDK源码调试
    分布式和集群的区别
    开发中model,entity和pojo的区别
    java并发编程_CountDownLanch(倒计数锁存器)应用场景
    Map 怎么排序
    java中Thread的 interrupt异常处理
    zookeeper节点失效重连机制
    java并发库_并发库知识点整理
  • 原文地址:https://www.cnblogs.com/xxoome/p/13280772.html
Copyright © 2011-2022 走看看