搜索
写经验 领红包
 > 设计

java多线程实现(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 timeoutThread.join with no timeoutLockSupport.park处于等待状态的线程正在等待另一个线程执行特定操作。常量类型:Time waiting
声明:public static final Thread.State TIMED_WAITING
描述:具有指定等待时间的等待线程的线程状态。由于在指定的正等待时间内调用以下方法之一,线程处于定时等待状态:Thread.sleepObject.wait with timeoutThread.join with timeoutLockSupport.parkNanosLockSupport.parkUntil常量类型:Terminated
声明:public static final Thread.State TERMINATED
描述:已终止线程的线程状态。线程已完成执行。
// Java program to demonstrate thread statesclass 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 - NEWState of thread1 after calling .start() method on it - RUNNABLEState of thread2 after creating it - NEWState of thread2 after calling .start() method on it - RUNNABLEState of thread2 after calling .sleep() method on it - TIMED_WAITINGState of thread1 while it called join() method on thread2 -WAITINGState of thread2 when it has finished it's execution - TERMINATED

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

本文内容由小思整理编辑!