美文网首页
java的线程,实现线程状态

java的线程,实现线程状态

作者: alcohol和cat | 来源:发表于2019-08-21 23:33 被阅读0次

在Java中,要获取线程的当前状态,请使用Thread.getState()方法获取线程的当前状态。Java提供了java.lang.Thread.State类,它定义了线程状态的ENUM常量,如下所示:

常量类型:NEW

声明:public static final Thread.State NEW

描述:尚未启动的线程的线程状态。

常量类型:Runnable

声明:public static final Thread.State RUNNABLE

描述:可运行线程的线程状态。处于可运行状态的线程正在Java虚拟机中执行,但它可能正在等待来自操作系统的其他资源,例如处理器。

常量类型:BLOCKED

声明:public static final Thread.State BLOCKED

描述:线程阻塞等待监视器锁定的线程状态。处于阻塞状态的线程正在等待监视器锁定以在调用Object.wait()之后输入同步块/方法或重新输入同步块/方法。

常量类型:WAITING

声明:public static final Thread.State WAITING

描述:等待线程的线程状态。等待线程的线程状态。由于调用以下方法之一,线程处于等待状态:

Object.wait with no timeout

Thread.join with no timeout

LockSupport.park

处于等待状态的线程正在等待另一个线程执行特定操作。

常量类型:Time waiting

声明:public static final Thread.State TIMED_WAITING

描述:具有指定等待时间的等待线程的线程状态。由于在指定的正等待时间内调用以下方法之一,线程处于定时等待状态:

Thread.sleep

Object.wait with timeout

Thread.join with timeout

LockSupport.parkNanos

LockSupport.parkUntil

常量类型:Terminated

声明:public static final Thread.State TERMINATED

描述:已终止线程的线程状态。线程已完成执行。

// Java program to demonstrate thread states

class thread implements Runnable

{

    public void run()

    {

        // moving thread2 to timed waiting state

        try

        {

            Thread.sleep(1500);

        }

        catch (InterruptedException e)

        {

            e.printStackTrace();

        }

        try

        {

            Thread.sleep(1500);

        }

        catch (InterruptedException e)

        {

            e.printStackTrace();

        }

        System.out.println("State of thread1 while it called join() method on thread2 -"+

            Test.thread1.getState());

        try

        {

            Thread.sleep(200);

        }

        catch (InterruptedException e)

        {

            e.printStackTrace();

        }    

    }

}

public class Test implements Runnable

{

    public static Thread thread1;

    public static Test obj;

    public static void main(String[] args)

    {

        obj = new Test();

        thread1 = new Thread(obj);

        // thread1 created and is currently in the NEW state.

        System.out.println("State of thread1 after creating it - " + thread1.getState());

        thread1.start();

        // thread1 moved to Runnable state

        System.out.println("State of thread1 after calling .start() method on it - " +

            thread1.getState());

    }

    public void run()

    {

        thread myThread = new thread();

        Thread thread2 = new Thread(myThread);

        // thread1 created and is currently in the NEW state.

        System.out.println("State of thread2 after creating it - "+ thread2.getState());

        thread2.start();

        // thread2 moved to Runnable state

        System.out.println("State of thread2 after calling .start() method on it - " +

            thread2.getState());

        // moving thread1 to timed waiting state

        try

        {

            //moving thread2 to timed waiting state

            Thread.sleep(200);

        }

        catch (InterruptedException e)

        {

            e.printStackTrace();

        }

        System.out.println("State of thread2 after calling .sleep() method on it - "+

            thread2.getState() );

        try

        {

            // waiting for thread2 to die

            thread2.join();

        }

        catch (InterruptedException e)

        {

            e.printStackTrace();

        }

        System.out.println("State of thread2 when it has finished it's execution - " +

            thread2.getState());

    }

}

输出:

State of thread1 after creating it - NEW

State of thread1 after calling .start() method on it - RUNNABLE

State of thread2 after creating it - NEW

State of thread2 after calling .start() method on it - RUNNABLE

State of thread2 after calling .sleep() method on it - TIMED_WAITING

State of thread1 while it called join() method on thread2 -WAITING

State of thread2 when it has finished it's execution - TERMINATED

说明:创建新线程时,线程处于NEW状态。在线程上调用.start()方法时,线程调度程序将其移动到Runnable状态。每当在线程实例上调用join()方法时,执行该语句的当前线程将等待此线程移动到Terminated状态。因此,在控制台上打印最终语句之前,程序调用thread2上的join(),使thread1等待,而thread2完成其执行并移至Terminated状态。thread1进入等待状态,因为它等待thread2完成它的执行,因为它在thread2上调用了join。

相关文章

  • Java线程简介

    本文将介绍Java线程的状态、线程的中断、线程间通信和线程的实现。 线程的状态 Java语言定义了6种不同的线程状...

  • 3个月面试20多次Java后端,总结的面试必会知识点

    Java多线程并发 先来看看并发知识库体系图: Java线程实现/创建方式? 4种线程池 ? 线程生命周期(状态)...

  • ExecutorService

    ExecutorService扩展和实现Executor。 java 线程池的5种状态 RUNNING 线程池...

  • JAVA多线程知识体系

    JAVA并发知识库JAVA线程实现/创建方式4种线程池线程生命周期(状态)终止线程4种方式sleep与wait 区...

  • java的线程,实现线程状态

    在Java中,要获取线程的当前状态,请使用Thread.getState()方法获取线程的当前状态。Java提供了...

  • 总结多线程与设计模式+synchronized+性能+高吞吐+死

    Java线程 Java语言的线程 何谓线程 线程启动 线程的暂时停止 线程的共享互斥 线程的协调 线程的状态转移 ...

  • java线程状态

    java线程状态 知识导读 JVM 实现都把 Java 线程一一映射到操作系统底层的线程上,把调度委托给了操作系统...

  • 线程中断

    Java的中断是一种协作机制,线程中断不会终止线程的运行,但是可以通过线程中断来实现终止线程运行。 线程在不同状态...

  • 线程状态

    java线程与操作系统线程状态略有不同。 1. 操作系统底层线程状态: 课本经典五状态: 2. JAVA线程状态:...

  • Java 线程的七种状态

    本篇感性地介绍一下 Java 线程的七种状态以及状态之间的转换 Java 线程状态转换图 Java 线程状态 在 ...

网友评论

      本文标题:java的线程,实现线程状态

      本文链接:https://www.haomeiwen.com/subject/umowsctx.html