千家信息网

Java中线程及线程状态是什么意思

发表于:2026-10-30 作者:千家信息网编辑
千家信息网最后更新 2026年10月30日,本篇内容主要讲解"Java中线程及线程状态是什么意思",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Java中线程及线程状态是什么意思"吧!线程线程(英语:
千家信息网最后更新 2026年10月30日Java中线程及线程状态是什么意思

本篇内容主要讲解"Java中线程及线程状态是什么意思",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Java中线程及线程状态是什么意思"吧!

线程

线程(英语:thread)是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。在Unix System V及SunOS中也被称为轻量进程(lightweight processes),但轻量进程更多指内核线程(kernel thread),而把用户线程(user thread)称为线程。

以上拷贝自维基百科

代码中任务、逻辑操作都依赖于线程,是java运行时最宝贵的资源

多线程一定程度可以增加cpu使用时间,压榨计算机资源提供更好的使用性能,一定程度也增加了资源的消耗如内存的增长、线程上下文数据切换的消耗、cup资源消耗,实际情况中我们应该根据业务场景合理的使用线程资源

Java线程生命周期

![image-20190712155311451](/Users/yugj/Library/Application Support/typora-user-images/image-20190712155311451.png)

https://www.geeksforgeeks.org/lifecycle-and-states-of-a-thread-in-java/contribute.geeksforgeeks.org/wp-content/uploads/threadLifeCycle.jpg

java.lang.Thread.State 定义了如下6种线程状态

/** * 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. New:刚创建,可被执行,并且未开始执行

  2. Runnable:正在执行或随时准备执行,例如多线程程序分配特定时间片给特定线程,特定线程执行短暂时间并暂停放弃cpu时间给其他线程,其他线程因此可以执行,这种场景线程是准备执行等待CPU时间,这种状态即Runnable

  3. Blocked:waiting for a monitor lock,处于需要获取其他线程锁定的同步资源,如等待io结束,这种状态在转变为Runnable之前无法执行,无法消耗cup时间片

  4. Waiting:等待其他线程执行特定操作,和Blocked类似

  5. Timed Waiting:线程调用等待执行场景,特定时间后执行,比较sleep,或者一些条件等待场景,如定时任务

  6. Terminated:正常或异常结束线程,将不分配CPU时间

模拟线程生命周期

1线程状态转换

public class DemonstrateThreadStates2 {    static Thread thread1;    public static void main(String[] args) {        //创建线程1        thread1 = new Thread(new TestThread1());        // thread1 创建后 NEW state.        System.out.println("State of thread1 after creating it - ">

控制台输出:

State of thread1 after creating it - NEWState of thread1 after calling .start() method on it - RUNNABLEState of thread2 after creating it - NEWState of thread2 after calling .start() method on it - RUNNABLEState of thread2 after calling .sleep() method on it - TIMED_WAITINGState of thread1 while it called join() method on thread2 -WAITINGState of thread2 when it has finished it's execution - TERMINATED

线程创建线程变成NEW状态,调用start启动线程变成Runnable,调用sleep阻塞当前线程吧变成Timed Waiting,thread2调用join将等待结束当前线程到父线程thread1,thread2线程将变成die,父线程thread1 等待线程thread2结束变成waiting

2模拟blocked场景

通过死锁模拟blocked场景

死锁条件

互斥使用:一个资源只能分配给一个线程不可剥夺:资源只能由占有者释放,申请者不能强制剥夺请求保持:线程申请资源时,保持对原有资源的占有循环等待:存在一个进程等待队列:{P1 , P2 , … , Pn}, 其中P1等待P2占有的资源,P2等待P3占有的资源,…,Pn等待P1占有的资源,形成一个进程等待环路代码
public class TestDeadLock implements Runnable {    // flag=1,占有对象o1,等待对象o2    // flag=0,占有对象o2,等待对象o1    public int flag = 1;    // 定义两个Object对象,模拟两个线程占有的资源    public static Object o1 = new Object();    public static Object o2 = new Object();    public static void main(String[] args) {        TestDeadLock deadLock1 = new TestDeadLock();        TestDeadLock deadLock2 = new TestDeadLock();        deadLock1.flag = 0;        deadLock2.flag = 1;        Thread thread1 = new Thread(deadLock1);        Thread thread2 = new Thread(deadLock2);        thread1.start();        thread2.start();    }    @Override    public void run() {        System.out.println("flag: " + flag);        // deadLock2占用资源o1,准备获取资源o2        if (flag == 1) {            synchronized (o1) {                try {                    Thread.sleep(1000);                } catch (InterruptedException e) {                    e.printStackTrace();                }                synchronized (o2) {                    System.out.println("1");                }            }        }        // deadLock1占用资源o2,准备获取资源o1        else if (flag == 0) {            synchronized (o2) {                try {                    Thread.sleep(1000);                } catch (InterruptedException e) {                    e.printStackTrace();                }                synchronized (o1) {                    System.out.println("0");                }            }        }    }}

参考文献

https://www.geeksforgeeks.org/lifecycle-and-states-of-a-thread-in-java/

https://www.jianshu.com/p/8cf78bf94f9d

到此,相信大家对"Java中线程及线程状态是什么意思"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

线程 资源 状态 时间 进程 场景 对象 准备 消耗 意思 任务 实际 分配 两个 代码 内容 单位 周期 更多 条件 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 耶加雪菲 喔噻互联网科技有限公司 高科技与互联网 互联网科技金融公司 深圳市运趣互联网科技有限公司 广州优美互联网科技 互联网经济与金融科技 厦门千工互联网科技有限公司 知域互联网科技有限公司大骗子 工业互联网等科技实现智能化赋能 科技互联网海报 互联网科技年终奖 宇信科技在杭州互联网排第几 陕西云淼互联网科技公司 广西科技产业互联网是什么 中国科技和美国互联网 广州三三互联网科技有限公司 盐城市攀科互联网科技有限公司 科技互联网专业务虚会材料 瑞安市蟠桃会互联网科技有限公司 互联网扶贫科技 上海致骋互联网科技 掌上梦互联网科技 滨湖区互联网智能科技产品供应 来此购互联网科技有限公司 工业互联网等科技实现智能化赋能 爱医互联网络科技 元气玛特互联网科技公司是外包吗 互联网科技包装股 如何强化互联网金融科技
0