java并发编程系列(01)线程的状态以及各状态之间的转换

简介

线程的状态和各个状态之间的转换:
线程的状态有以下六种:

关于线程状态这一块我看过一些资料,有视频资料有网上的博客,有说其中状态的也有说五种状态的,我这里所说的六种状态是JDK1.8中源码中所定义的六中状态。这里引入一下JDK1.8中的注释说明一下:
A thread can be in only one state at a given point in time.
These states are virtual machine states which do not reflect
any operating system thread states.

1.创建 2.运行 3.阻塞 4. 等待 5.超时等待 6.终止

线程的生命周期

1.NEW
至今尚未启动的线程的状态。
2.RUNNABLE
可运行线程的线程状态。
3.BLOCKED
受阻塞并且正在等待监视器锁的某一线程的线程状态。
5.TIMED_WAITING
具有指定等待时间的某一等待线程的线程状态。
4.WAITING
某一等待线程的线程状态。
6.TERMINATED
已终止线程的线程状态。

(引入JDK API文档)

		/**
         * 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.
         * 可运行线程的线程状态。处于可运行状态的某一线程正在 Java 虚拟机中运行,
         * 但它可能正在等待操作系统中的其他资源,比如处理器。
         */
        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}.
         * 受阻塞并且正在等待监视器锁的某一线程的线程状态。
         * 处于受阻塞状态的某一线程正在等待监视器锁,以便进入一个同步的块/方法,
         * 或者在调用 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>
         *
         * <p>A thread in the waiting state is waiting for another thread to
         * perform a particular action.
         *
         * For example, a thread that has called <tt>Object.wait()</tt>
         * on an object is waiting for another thread to call
         * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
         * that object. A thread that has called <tt>Thread.join()</tt>
         * is waiting for a specified thread to terminate.
         * 处于等待状态的线程正等待另一个线程,以执行特定操作。
         *  例如,已经在某一对象上调用了 Object.wait() 的线程正等待另一个线程,
         * 以便在该对象上调用 Object.notify() 或 Object.notifyAll()。
         * 已经调用了 Thread.join() 的线程正在等待指定线程终止。 


         */
        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 Thread(new Runnable() {
            @Override
            public void run() {
                
            }
        }).start();

推荐使用:

       MineRunnable mineRunnable=new MineRunnable();
        Thread mineThread=new Thread(new MineRunnable());
        mineThread.start();
    }
    public static class MineRunnable implements Runnable{

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName()+Thread.currentThread().getThreadGroup().getName());

        }
    }

因为Thread实现了Runnable接口,所以也可以通过充血Thread的Run方法来实现

public class MineThread extends Thread {
    @Override
    public void run() {;
        System.out.println(Thread.currentThread().getName());
    }
    public static void main(String[] args){
        MineThread mineThread=new MineThread();
        mineThread.start();
    }
}

IllegalMonitorStateException

原理分析

在这里插入图片描述

源码分析(待完善)

结束语

写的不好的地方,多多见谅。喜欢的赏我一个赞

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值