java 多线程 状态_java多线程,用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。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值