溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java多線程中線程狀態(tài)原理的示例分析

發(fā)布時間:2021-06-10 11:32:18 來源:億速云 閱讀:107 作者:小新 欄目:編程語言

小編給大家分享一下Java多線程中線程狀態(tài)原理的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

java.lang.Thread.State枚舉定義了6種線程狀態(tài)。

  • NEW: 尚未啟動(start)的線程的線程狀態(tài)

  • RUNNABLE: 運行狀態(tài),但線程可能正在JVM中執(zhí)行,也可能在等待CPU調(diào)度

  • BLOCKED: 線程阻塞,等待監(jiān)視器鎖以進(jìn)入同步代碼塊/方法

  • WAITING: 等待狀態(tài)。使用以下不帶超時的方式時會進(jìn)入:Object.wait、Thread.join、LockSupport.park。等待狀態(tài)的線程需要另一個線程激活。

  • TIMED_WAITING: 具有指定等待時間的等待狀態(tài),使用以下帶超時的方式:Thread.sleep、Object.wait、Thread.join、LockSupport.parkNanos、LockSupport.parkUntil。

  • TERMINATED: 終止?fàn)顟B(tài)。線程正常執(zhí)行完畢或者發(fā)生異常。

狀態(tài)切換

狀態(tài)條件狀態(tài)條件狀態(tài)
NEW-(啟動)-》RUNNABLE-(等待鎖)-》BLOCKED


RUNNABLE《-(拿到鎖)-BLOCKED


RUNNABLE-(等待其他線程通知)-》WAITING


RUNNABLE《-(收到其他線程通知)-WAITING
 


RUNNABLE-(有超時時間的,等待其他線程通知)-》TIMED_WAITING
 


RUNNABLE《-(收到其他線程通知,或者等待超時)-TIMED_WAITING
 


RUNNABLE-(執(zhí)行結(jié)束)-》TERMINATED

線程切換代碼示例

/**
 * 多線程運行狀態(tài)切換示例
 */
public class Demo2 {

  public static void main(String[] args) throws Exception{
    //第一種狀態(tài)切換 新建->運行->終止
    System.out.println("###第一種狀態(tài)切換 新建->運行->終止###");
    Thread thread1 = new Thread(()->{
      System.out.println("thread1當(dāng)前狀態(tài):" + Thread.currentThread().getState());
      System.out.println("thread1執(zhí)行完畢");
    });
    System.out.println("調(diào)用start之前,thread1當(dāng)前狀態(tài):" + thread1.getState());
    thread1.start();
    Thread.sleep(2000L);  //等待thread1執(zhí)行結(jié)束
    System.out.println("等待thread1執(zhí)行結(jié)束,當(dāng)前狀態(tài):" + thread1.getState());
    System.out.println();
//    thread1.start();    //此時會拋出java.lang.IllegalThreadStateException

    //第二種狀態(tài)切換 新建->運行->等待->運行->終止
    System.out.println("###第二種狀態(tài)切換 新建->運行->等待->運行->終止###");
    Thread thread2 = new Thread(()->{
      try {
        Thread.sleep(1500L);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      System.out.println("thread2當(dāng)前狀態(tài):" + Thread.currentThread().getState());
      System.out.println("thread2執(zhí)行完畢");
    });
    System.out.println("調(diào)用start之前,thread2當(dāng)前狀態(tài):" + thread2.getState());
    thread2.start();
    System.out.println("調(diào)用start之后,thread2當(dāng)前狀態(tài):" + thread2.getState());
    Thread.sleep(200L);  //等待一段時間令thread2進(jìn)入sleep方法
    System.out.println("調(diào)用start之后,等待200毫秒,thread2當(dāng)前狀態(tài):" + thread2.getState());
    Thread.sleep(2000L);  //等待thread2執(zhí)行結(jié)束
    System.out.println("等待thread2執(zhí)行結(jié)束,當(dāng)前狀態(tài):" + thread2.getState());
    System.out.println();

    //第三種狀態(tài)切換 新建->運行->阻塞->運行->終止
    System.out.println("###第三種狀態(tài)切換 新建->運行->阻塞->運行->終止###");
    Thread thread3 = new Thread(()->{
      synchronized (Demo2.class){
        System.out.println("thread3當(dāng)前狀態(tài):" + Thread.currentThread().getState());
        System.out.println("thread3執(zhí)行完畢");
      }
    });
    synchronized (Demo2.class){
      System.out.println("調(diào)用start之前,thread3當(dāng)前狀態(tài):" + thread3.getState());
      thread3.start();
      System.out.println("調(diào)用start之后,thread3當(dāng)前狀態(tài):" + thread3.getState());
      Thread.sleep(200L);
      System.out.println("等待200毫秒,thread3等待獲取鎖,thread3當(dāng)前狀態(tài):" + thread3.getState());
    }
    Thread.sleep(1000L);    //等待thread3執(zhí)行完畢
    System.out.println("等待1秒,令thread3獲取鎖后執(zhí)行方法,thread3當(dāng)前狀態(tài):" + thread3.getState());
  }
}

以上是“Java多線程中線程狀態(tài)原理的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI