廊坊市网站,建站行业如何快速成第一单,媒体135网站,wordpress响应案例文章目录 前言传统线程模型#xff08;操作系统#xff09;中线程状态Java线程中的状态线程的运行流程 前言
首先我们要知道#xff0c;在传统#xff08;操作系统#xff09;的线程模型中线程被分为五种状态#xff0c;在java线程中#xff0c;线程被分为六种状态。
… 文章目录 前言传统线程模型操作系统中线程状态Java线程中的状态线程的运行流程 前言
首先我们要知道在传统操作系统的线程模型中线程被分为五种状态在java线程中线程被分为六种状态。
传统线程模型操作系统中线程状态 线程的五种状态 新建new 创建了一个新的线程对象 就绪runnable 调用线程的start()方法处于就绪状态 运行running 获得了CPU时间片执行程序代码 就绪状态是进入到运行状态的唯一入口 阻塞block 因为某种原因线程放弃对CPU的使用权停止执行直到进入就绪状态在有可能再次被CPU调度 阻塞又分为三种 等待阻塞运行状态的线程执行wait方法JVM会把线程放在等待队列中使本线程进入阻塞状态。 同步阻塞线程在获得synchronized同步锁失败JVM会把线程放入锁池中线程进入同步阻塞。对于锁池和等待池可以看这篇文章 其他阻塞调用线程的sleep()或者join()后线程会进入道阻塞状态当sleep超时或者join终止或超时线程重新转入就绪状态 死亡dead 线程run()、main()方法执行结束或者因为异常退出了run()方法则该线程结束生命周期 死亡的线程不可再次复生
Java线程中的状态
通过查看Thread类的State方法我们可以看到Java线程其实是六种状态
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:* ul* li{link Object#wait() Object.wait} with no timeout/li* li{link #join() Thread.join} with no timeout/li* li{link LockSupport#park() LockSupport.park}/li* /ul** pA thread in the waiting state is waiting for another thread to* perform a particular action.** For example, a thread that has called ttObject.wait()/tt* on an object is waiting for another thread to call* ttObject.notify()/tt or ttObject.notifyAll()/tt on* that object. A thread that has called ttThread.join()/tt* 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:* ul* li{link #sleep Thread.sleep}/li* li{link Object#wait(long) Object.wait} with timeout/li* li{link #join(long) Thread.join} with timeout/li* li{link LockSupport#parkNanos LockSupport.parkNanos}/li* li{link LockSupport#parkUntil LockSupport.parkUntil}/li* /ul*/TIMED_WAITING,/*** Thread state for a terminated thread.* The thread has completed execution.*/TERMINATED;}我们可以看到线程实际上是分为六种状态的既 初始状态NEW 线程被构建但是还没有调用start方法 运行状态RUNNABLE Java线程把操作系统中就绪和运行两种状态统一称为“运行中” 阻塞状态BLOCKED 表示线程进入等待状态也就是线程因为某种原因放弃了CPU的使用权阻塞也分为几种情况当一个线程试图获取一个内部的对象锁非java.util.concurrent库中的锁而该锁被其他线程持有则该线程进入阻塞状态。 等待阻塞运行的线程执行了Thread.sleep、wait、join等方法JVM会把当前线程设置为等待状态当sleep结束join线程终止或者线程被唤醒后该线程从等待状态进入阻塞状态重新占用锁后进行线程恢复 同步阻塞运行的线程在获取对象的同步锁时若该同步锁被其他线程锁占用了那么JVM会把当前项城放入到锁池中 其他阻塞发出I/O请求JVM会把当前线程设置为阻塞状态当I/O处理完毕则线程恢复 等待WAITING 等待状态没有超时时间无限等待要被其他线程或者有其他的中断操作 执行wait、join、LockSupport.park() 超时等待TIME_WAITING 与等待不同的是不是无限等待超时后自动返回 执行sleep带参数的wait等可以实现 终止Teminated 代表线程执行完毕
线程的运行流程 线程首先被new创建进入初始状态
然后线程调用start方法进入就绪状态
这里要注意线程只要抢占了cpu时间片可以不用获取全部的锁就可以运行但是当运行到需要的锁没有获得时会进入阻塞状态
当一个线程被sleep后线程会先进入超时等待状态当时间结束后会先进入等待阻塞状态当有锁以后再进入就绪状态