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:
         * 
    *
  • {@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. * 处于等待状态的线程正等待另一个线程,以执行特定操作。 * 例如,已经在某一对象上调用了 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: *

    *
  • {@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;//终止

备注:在给定时间点上,一个线程只能处于一种状态。这些状态是虚拟机状态,它们并没有反映所有操作系统线程状态。

名词解释

如何使用

常见使用:

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

原理分析

java并发编程系列(01)线程的状态以及各状态之间的转换_第1张图片

源码分析(待完善)

结束语

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

你可能感兴趣的:(java并发编程系列,线程状态,线程状态转换,多线程编程基础)