java的线程,实现线程状态

在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。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 文章来源:http://www.54tianzhisheng.cn/2017/06/04/Java-Thread/...
    beneke阅读 1,534评论 0 1
  • Java多线程学习 [-] 一扩展javalangThread类 二实现javalangRunnable接口 三T...
    影驰阅读 2,994评论 1 18
  • 本文主要讲了java中多线程的使用方法、线程同步、线程数据传递、线程状态及相应的一些线程函数用法、概述等。 首先讲...
    李欣阳阅读 2,503评论 1 15
  • 一、认识多任务、多进程、单线程、多线程 要认识多线程就要从操作系统的原理说起。 以前古老的DOS操作系统(V 6....
    GT921阅读 1,032评论 0 3
  • Thread是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程...
    Wu巧不成阅读 431评论 0 0