美文网首页
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的线程,实现线程状态

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