java高性能编程基础- java中线程的六种状态,线程状态变化的几种类型

Posted 1 1=王

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java高性能编程基础- java中线程的六种状态,线程状态变化的几种类型相关的知识,希望对你有一定的参考价值。

从源码看Thread中定义的六种线程状态

Thread类位于java.lang包下,在Thread类中有一个枚举类型State,State中定义了线程的六种状态。源码如下:

java.lang.Thread.State

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>
         *
         * <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.
         */
        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;
    

6中线程状态:

  • 1、NEW
    尚未启动的线程的线程状态。
  • 2、RUNNABLE
    可运行线程的线程状态。
    可运行的线程状态正在Java虚拟机中执行,但它可能等待操作系统的其他资源如处理器。
  • 3、BLOCKED
    线程阻塞等待监视器锁定的线程状态。
    处于synchronized同步代码块或方法中被阻塞。
  • 4、WAITING
    等待线程的线程状态。
    处于等待状态的线程正在等待另一个线程执行特定的操作。
  • 5、TIMED_WAITING
    具有指定等待时间的等待线程的线程状态。
  • 6、TERMINATED
    终止线程的线程状态。
    线程正常完成执行或者出现异常。

注:线程在给定时间点只能处于一种状态。这些状态是虚拟机状态,不反映任何操作系统线程状态。

调用getState方法可以得到当前线程的状态。

public class ThreadTest 
    public static void main(String[] args) 
        Thread thread1 = new MyThread();

        System.out.println("thread1的线程状态:" + thread1.getState());
        thread1.start();
        System.out.println("thread1的线程状态:" + thread1.getState());

    


class MyThread extends Thread
    @Override
    public void run() 
        System.out.println(this.getName() + " is running。。。");
    

线程状态的变化

1.线程正常运行结束的状态变化

public class ThreadState 
    public static void main(String[] args) throws InterruptedException 
        Thread thread1 = new Thread(new Runnable() 
            @Override
            public void run() 
                System.out.println("当前线程正在执行-------");
                System.out.println("线程正在执行时的线程状态:" + Thread.currentThread().getState().toString());
            
        );
        System.out.println("创建线程后,调用start前的线程状态:" + thread1.getState().toString());
        thread1.start();
        Thread.sleep(2000L); // 等待thread1执行结束,再看状态
        System.out.println("---等待两秒,线程执行结束后的状态:" + thread1.getState().toString());
        //thread1.start(); //当线程终止之后,再进行调用,会抛出IllegalThreadStateException异常
    


程序运行结果显示:

  • 当一个线程被创建时(还未调用start方法)处于NEW状态;
  • 调用start方法后,线程开始执行,此时线程处于RUNNABLE状态;
  • 线程继续执行,一段时间后,线程正常执行结束,此时线程处于TERMINATED状态。
    (新建 -> 运行 -> 终止,NEW-> RUNNABLE-> TERMINATED)

注意:当线程执行结束之后,再进行调用,会抛出IllegalThreadStateException异常

2.线程执行过程中等待一段时间再继续执行结束的状态变化

public class ThreadState 
    public static void main(String[] args) throws InterruptedException 
        Thread thread2 = new Thread(new Runnable() 
            @Override
            public void run() 
                System.out.println("当前线程正在执行-------,需要花费两秒时间------");
                try    // 将线程2移动到等待状态,2s后自动唤醒
                    Thread.sleep(2000);
                 catch (InterruptedException e) 
                    e.printStackTrace();
                
                System.out.println("线程正在执行时的线程状态:" + Thread.currentThread().getState().toString());
            
        );
        System.out.println("创建线程后,调用start前的线程状态:" + thread2.getState().toString());
        thread2.start();
        Thread.sleep(200L); // 等待200毫秒,再看状态
        System.out.println("等待200毫秒(线程还没执行结束),线程的状态:" + thread2.getState().toString());
        Thread.sleep(3000L); // 再等待3秒,让thread2执行完毕,再看状态
        System.out.println("等待3秒(线程执行结束),线程的状态:" + thread2.getState().toString());
    

程序运行结果显示:

  • 当一个线程被创建时(还未调用start方法)处于NEW状态;
  • 调用start方法后,线程开始执行,此时线程处于RUNNABLE状态;
  • 当线程运行过程中需要等待一段时间时,线程进入TIMED_WAITING状态
  • 线程继续执行,一段时间后,线程正常执行结束,此时线程处于TERMINATED状态。
    (新建 -> 运行 -> 等待 -> 运行 -> 终止,NEW-> RUNNABLE->TIMED_WAITING->RUNNABLE-> TERMINATED)

3.线程执行过程中等待其他线程被阻塞,然后继续执行到结束的状态变化

public class ThreadState 
    public static void main(String[] args) throws InterruptedException 
        Thread thread3 = new Thread(new Runnable() 
            @Override
            public void run() 
                synchronized (ThreadState.class) 
                    System.out.println("当前线程正在执行-------");
                    System.out.println("线程正在执行时的线程状态:" + Thread.currentThread().getState().toString());
                
            
        );
        synchronized (ThreadState.class) 
            System.out.println("没调用start方法,thread3当前状态:" + thread3.getState().toString());
            thread3.start();
            System.out.println("调用start方法,thread3当前状态:" + thread3.getState().toString());
            Thread.sleep(200L);
            System.out.println("等待200毫秒,线程遇到锁被堵塞时的状态:" + thread3.getState().toString());
        
        Thread.sleep(3000L); // 再等待3秒,让thread3执行完毕,再看状态
        System.out.println("等待3秒,线程抢到锁并执行结束时的状态:" + thread3.getState().toString());
    

程序运行结果显示:

  • 当一个线程被创建时(还未调用start方法)处于NEW状态;
  • 调用start方法后,线程开始执行,此时线程处于RUNNABLE状态;
  • 当线程遇到锁被堵塞时线程进入BLOCKED状态
  • 线程继续执行,一段时间后,线程正常执行结束,此时线程处于TERMINATED状态。
    (新建 -> 运行 -> 阻塞 -> 运行 -> 终止,NEW-> RUNNABLE->BLOCKED->RUNNABLE-> TERMINATED)

以上是关于java高性能编程基础- java中线程的六种状态,线程状态变化的几种类型的主要内容,如果未能解决你的问题,请参考以下文章

Java线程的六种状态

JUC并发编程 -- 回顾多线程(线程的六种状态 & wait / sleep 的区别)

2020年的六种编程语言排名中,java排第几只有不到1%的人知道

Java并发编程的艺术读书笔记——Java并发编程基础

Java多线程-线程状态

Java线程及线程池状态