线程的状态

Thread.State枚举类型中定义了线程的六种状态:NEWRUNNABLEBLOCKEDWAITINGTIMED_WAITINGTERMINATED

线程在某一时刻只能拥有一种状态,但是在线程的整个生命周期,线程的状态会发生变化。

public enum State {
NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED;
}

各状态的说明

NEW

NEW状态是线程已经被创建,但还没调用start()。此时的线程是不可运行的,CPU将不会为其分配时间。

RUNNABLE

当新创建的线程调用了start(),线程便进入了RUNNABLE状态。

RUNNABLE状态是指可以获得CPU运行时间的状态,如果线程在此状态下,线程有两种子状态,一种是等待CPU时间,另一种是获得了CPU时间在执行代码。

BLOCKED

BLOCKED状态是线程无法获取对象锁时的状态。此状态下线程会阻塞,当线程成功获取到锁,线程将切换为RUNNABLE状态。

BLOCKED状态无法获得CPU运行时间。

WAITING

WAITING状态是指是指线程在执行过程中,主动出让自己CPU运行时间,让其他线程先执行,自己等待其它线程的特定操作后再恢复执行。

TIMED_WAITING

TIMED_WAITINGWAITING状态相似,TIMED_WAITING增加了时间限制,其实没有外部信号,在等待时间超时后,线程也会恢复。

TERMINATED

TERMINATED是线程的终止态,当线程执行完自己的任务,或在执行任务中发生了异常,线程都会进入TERMINATED,表示线程已经到了生命周期的末尾。

下图是关于线程间各状态切换的过程及发生状态切换的一些条件。

操作 操作前线程状态 操作后线程状态 是否出让CPU时间 是否需要先持有对象锁 是否释放对象锁
new Thread() NEW
Thread.start() NEW RUNNABLE
synchronized能得到对象锁 RUNNABLE RUNNABLE
synchronized无法得到对象锁 RUNNABLE BLOCKED
Thread.join() RUNNABLE WAITING
Thread.join(t) RUNNABLE TIMED_WAITING
Thread.sleep(t) RUNNABLE TIMED_WAITING
Object.wait() RUNNABLE WAITING
Object.wait(t) RUNNABLE TIMED_WAITING
Object.notify() / Object.notifyAll() RUNNABLE RUNNABLE
Lock.lock() RUNNABLE WAITING
Lock.tryLock(t) RUNNABLE TIMED_WAITING
LockSupport.park() RUNNABLE WAITING
LockSupport.parkNanos(t)/LockSupport.parkUntil(t) RUNNABLE TIMED_WAITING
执行结束/执行异常 RUNNABLE TERMINATED

以下是一些测试代码,可以运行下清晰的了解到状态。

各状态切换测试:

public class ThreadStateTest {

    public static void main(String[] args){
threadStateNew(); workingThread(); threadStateTerminate(); threadBlockedByLock(); threadBlockedBySynchronized(); threadSleep(); threadWait(); threadTimedWait();
} private static void threadStateNew(){
System.out.println("--------------------------");
System.out.print("Never Start Thread State:");
Thread thread = new Thread(()->{ }, "Thread Never Start");
//print NEW
System.out.println(thread.getState());
System.out.println("--------------------------");
} private static void workingThread(){
System.out.println("--------------------------");
Thread thread = new Thread(()->{
for(int i=0; i<100; i++){
doSomeElse();
}
}); thread.start(); doSomeElse();
//print RUNNABLE
System.out.println("Working Thread State:" + thread.getState());
System.out.println("--------------------------");
} private static void threadStateTerminate(){
System.out.println("--------------------------");
System.out.print("Finish Job Thread State:");
Thread thread = new Thread(()->{ }, "Thread Finish Job");
thread.start(); try {
//Main Thread Will Wait util this thread finished job
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
//print TERMINATED
System.out.println(thread.getState());
System.out.println("--------------------------");
} private static void threadBlockedByLock(){
System.out.println("--------------------------");
System.out.print("Thread State Blocked By Lock:");
ReentrantLock lock = new ReentrantLock();
Thread thread = new Thread(()->{
lock.lock();
}, "Blocked Thread"); lock.lock(); thread.start(); doSomeElse();
//print WAITING
System.out.println(thread.getState()); lock.unlock();
System.out.println("--------------------------");
} private static void threadBlockedBySynchronized(){
System.out.println("--------------------------");
System.out.print("Thread Blocked By Synchronized:");
Thread thread = new Thread(()->{
synchronized (ThreadStateTest.class){ }
}, "Blocked by Synchronized Thread"); synchronized (ThreadStateTest.class){
thread.start();
doSomeElse();
//print BLOCKED
System.out.println(thread.getState());
}
System.out.println("--------------------------");
} private static void threadSleep(){
System.out.println("--------------------------");
System.out.print("Sleeping Thread:");
Thread thread = new Thread(()->{
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "Thread sleep"); thread.start();
doSomeElse();
//print TIMED_WAITING
System.out.println(thread.getState());
System.out.println("--------------------------");
} private static void threadWait(){
System.out.println("--------------------------");
System.out.print("Thread Waiting:");
Object lock = new Object();
Thread threadA = new Thread(()->{
synchronized (lock){
try {
lock.wait(); for(int i=0; i<100; i++){
doSomeElse();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} }, "Thread Waiting"); Thread threadB = new Thread(()->{
synchronized (lock){
//print WAITING
System.out.println("Before Notify, Thread A State:" + threadA.getState());
lock.notify();
//print BLOCKED
System.out.println("After Notify, Thread A State:" + threadA.getState());
}
}); threadA.start();
doSomeElse();
threadB.start(); try {
threadB.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
//print RUNNABLE
System.out.println("After Thread B finish job, Thread A State:" + threadA.getState());
System.out.println("--------------------------");
} private static void threadTimedWait(){
System.out.println("--------------------------");
System.out.print("Thread Waiting:");
Object lock = new Object();
Thread threadA = new Thread(()->{
synchronized (lock){
try {
lock.wait(1000); for(int i=0; i<100; i++){
doSomeElse();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} }, "Thread Waiting"); Thread threadB = new Thread(()->{
synchronized (lock){
//print TIMED_WAITING
System.out.println("Before Notify, Thread A State:" + threadA.getState());
lock.notify();
//print BLOCKED
System.out.println("After Notify, Thread A State:" + threadA.getState());
}
}); threadA.start();
doSomeElse();
threadB.start(); try {
threadB.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
//print RUNNABLE
System.out.println("After Thread B finish job, Thread A State:" + threadA.getState());
System.out.println("--------------------------");
}
/**
* take some times, let the thread get cpu time
*/
private static void doSomeElse(){
double meanless = 0d;
for(int i=0; i<10000; i++){
meanless += Math.random();
}
} }

CPU时间运行测试:

public class ThreadCPUTimeTest {

    public static void main(String[] args) {
testBlockedThreadCPUTime();
} protected static void testBlockedThreadCPUTime() {
Object lock = new Object(); Thread threadA = new Thread(() -> {
synchronized (lock) {
doSomethingElse();
}
}, "ThreadA: Blocked because of synchronized"); Thread threadB = new Thread(() -> {
synchronized (lock) {
try {
threadA.start();
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "ThreadB: With Monitor But Sleep"); threadB.start();
//Main Thread Executing Job
for (int i = 0; i < 100000; i++) {
doSomethingElse();
}
} private static void doSomethingElse() {
double meanless = 0d; for (int i = 0; i < 10000; i++) {
meanless += Math.random();
}
}
}

用VISUALVM可以统计CPU时间:

详细代码可以GitHub

线程状态及各状态下与锁和CPU的关系的更多相关文章

  1. java 线程的几种状态

    java thread的运行周期中, 有几种状态, 在 java.lang.Thread.State 中有详细定义和说明: NEW 状态是指线程刚创建, 尚未启动 RUNNABLE 状态是线程正在正常 ...

  2. 在java中怎样实现多线程?线程的4种状态

    一.在java中怎样实现多线程? extends Thread implement Runnable 方法一:继承 Thread 类,覆盖方法 run(),我们在创建的 Thread 类的子类中重写 ...

  3. Java线程Thread的状态解析以及状态转换分析 多线程中篇(七)

    线程与操作系统中线程(进程)的概念同根同源,尽管千差万别. 操作系统中有状态以及状态的切换,Java线程中照样也有. State 在Thread类中有内部类 枚举State,用于抽象描述Java线程的 ...

  4. 透彻讲解,Java线程的6种状态及切换

    Java中线程的状态分为6种. 1. 初始(NEW):新创建了一个线程对象,但还没有调用start()方法.2. 运行(RUNNABLE):Java线程中将就绪(ready)和运行中(running) ...

  5. Java线程的5种状态及切换(透彻讲解)-京东面试

    一.Thread的几个重要方法: 我们先了解一下Thread的几个重要方法. a.start()方法,开始执行该线程:b.stop()方法,强制结束该线程执行:c.join方法,等待该线程结束.d.s ...

  6. 【转】java 线程的几种状态

    java thread的运行周期中, 有几种状态, 在 java.lang.Thread.State 中有详细定义和说明: NEW 状态是指线程刚创建, 尚未启动 RUNNABLE 状态是线程正在正常 ...

  7. 线程的几种状态转换<转>

    线程在一定条件下,状态会发生变化.线程一共有以下几种状态: 1.新建状态(New):新创建了一个线程对象. 2.就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法. ...

  8. Java 多线程 线程的五种状态,线程 Sleep, Wait, notify, notifyAll

    一.先来看看Thread类里面都有哪几种状态,在Thread.class中可以找到这个枚举,它定义了线程的相关状态: public enum State { NEW, RUNNABLE, BLOCKE ...

  9. Java线程基础知识(状态、共享与协作)

    1.基础概念 CPU核心数和线程数的关系 核心数:线程数=1:1 ;使用了超线程技术后---> 1:2 CPU时间片轮转机制 又称RR调度,会导致上下文切换 什么是进程和线程 进程:程序运行资源 ...

随机推荐

  1. php 分页使用limit还是用mysql_data_seek()呢?

    目前大部分教程中介绍的时LIMIT方法,使用这种方法要认识到以下几点: (1)limit不是标准的sql语句; (2)如果选择使用limit,那么您就必须首先向数据库发送一个查询语句来获取记录的总数, ...

  2. idle中上传jar包并使用的方法

    创建一个lib目录,将jar包拉到该目录下. 需要导入的Jar包上,点击右键,选择Add as Library…

  3. sparkRdd driver和excuter

    //1 从内存中创建makeRdd,底层实现就是parallelize val rdd=sc.makeRDD(Array(1,2,"df",55)) //2 从中创建paralle ...

  4. std::string构造函数

    string(); string (const string& str); string (const string& str, size_t pos, size_t len = np ...

  5. Centos7 编译安装 Libmcrypt 库

    0x00 先下载 libmcrypt 库源码 libmcrypt-2.5.8.tar.gz 或者去这里 libmcrypt 下载你需要的版本. 0x01 将下载的源码解压到文件夹 tar -zxvf ...

  6. Apache SkyWalking

    Apache SkyWalking 什么是 SkyWalking SkyWalking 是观察性分析平台和应用性能管理系统. 提供分布式追踪.服务网格遥测分析.度量聚合和可视化一体化解决方案. 支持J ...

  7. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十四)之Type Information

    Runtime type information (RTTI) allow you to discover and use type information while a program is ru ...

  8. centos7用户管理及root忘记密码恢复

    查看用户相关命令:#id 用户和组的信息#whoami #查看当前有效用户名#who #显示目前登入系统的用户信息.#w # w 命令用于显示已经登陆系统的用户列表#users #用于显示当前登录系统 ...

  9. 绝地求生模拟登陆!高难度JS解密教程,Python高级爬虫开发,

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取htt ...

  10. L20 梯度下降、随机梯度下降和小批量梯度下降

    airfoil4755 下载 链接:https://pan.baidu.com/s/1YEtNjJ0_G9eeH6A6vHXhnA 提取码:dwjq 梯度下降 (Boyd & Vandenbe ...