您好,登錄后才能下訂單哦!
本篇內容主要講解“Java中線程及線程狀態是什么意思”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Java中線程及線程狀態是什么意思”吧!
線程(英語:thread)是操作系統能夠進行運算調度的最小單位。它被包含在進程之中,是進程中的實際運作單位。一條線程指的是進程中一個單一順序的控制流, 一個進程中可以并發多個線程,每條線程并行執行不同的任務。在Unix System V及SunOS中也被稱為輕量進程(lightweight processes), 但輕量進程更多指內核線程(kernel thread),而把用戶線程(user thread)稱為線程。
以上拷貝自維基百科
代碼中任務、邏輯操作都依賴于線程,是java運行時最寶貴的資源
多線程一定程度可以增加cpu使用時間,壓榨計算機資源提供更好的使用性能,一定程度也增加了資源的消耗如內存的增長、線程上下文數據切換的消耗、cup資源消耗,實際情況中我們應該根據業務場景合理的使用線程資源
![image-20190712155311451](/Users/yugj/Library/Application Support/typora-user-images/image-20190712155311451.png)
https://www.geeksforgeeks.org/lifecycle-and-states-of-a-thread-in-java/contribute.geeksforgeeks.org/wp-content/uploads/threadLifeCycle.jpg
java.lang.Thread.State 定義了如下6種線程狀態
/** * Thread state for a thread which has not yet started. */ NEW, /** * Thread state for a runnable thread. A thread in the runnable * state is executing in the Java virtual machine but it may * be waiting for other resources from the operating system * such as processor. */ RUNNABLE, /** * Thread state for a thread blocked waiting for a monitor lock. * A thread in the blocked state is waiting for a monitor lock * to enter a synchronized block/method or * reenter a synchronized block/method after calling * {@link Object#wait() Object.wait}. */ BLOCKED, /** * Thread state for a waiting thread. * A thread is in the waiting state due to calling one of the * following methods: * <ul> * <li>{@link Object#wait() Object.wait} with no timeout</li> * <li>{@link #join() Thread.join} with no timeout</li> * <li>{@link LockSupport#park() LockSupport.park}</li> * </ul> * * <p>A thread in the waiting state is waiting for another thread to * perform a particular action. * * For example, a thread that has called <tt>Object.wait()</tt> * on an object is waiting for another thread to call * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on * that object. A thread that has called <tt>Thread.join()</tt> * is waiting for a specified thread to terminate. */ WAITING, /** * Thread state for a waiting thread with a specified waiting time. * A thread is in the timed waiting state due to calling one of * the following methods with a specified positive waiting time: * <ul> * <li>{@link #sleep Thread.sleep}</li> * <li>{@link Object#wait(long) Object.wait} with timeout</li> * <li>{@link #join(long) Thread.join} with timeout</li> * <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li> * <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li> * </ul> */ TIMED_WAITING, /** * Thread state for a terminated thread. * The thread has completed execution. */ TERMINATED;
New:剛創建,可被執行,并且未開始執行
Runnable:正在執行或隨時準備執行,例如多線程程序分配特定時間片給特定線程,特定線程執行短暫時間并暫停放棄cpu時間給其他線程,其他線程因此可以執行,這種場景線程是準備執行等待CPU時間,這種狀態即Runnable
Blocked:waiting for a monitor lock,處于需要獲取其他線程鎖定的同步資源,如等待io結束,這種狀態在轉變為Runnable之前無法執行,無法消耗cup時間片
Waiting:等待其他線程執行特定操作,和Blocked類似
Timed Waiting:線程調用等待執行場景,特定時間后執行,比較sleep,或者一些條件等待場景,如定時任務
Terminated:正常或異常結束線程,將不分配CPU時間
1線程狀態轉換
public class DemonstrateThreadStates2 { static Thread thread1; public static void main(String[] args) { //創建線程1 thread1 = new Thread(new TestThread1()); // thread1 創建后 NEW state. System.out.println("State of thread1 after creating it - ">
控制臺輸出:
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,調用sleep阻塞當前線程吧變成Timed Waiting,thread2調用join將等待結束當前線程到父線程thread1,thread2線程將變成die,父線程thread1 等待線程thread2結束變成waiting
2模擬blocked場景
通過死鎖模擬blocked場景
死鎖條件
互斥使用:一個資源只能分配給一個線程 不可剝奪:資源只能由占有者釋放,申請者不能強制剝奪 請求保持:線程申請資源時,保持對原有資源的占有 循環等待:存在一個進程等待隊列:{P1 , P2 , … , Pn}, 其中P1等待P2占有的資源,P2等待P3占有的資源,…,Pn等待P1占有的資源,形成一個進程等待環路 代碼
public class TestDeadLock implements Runnable { // flag=1,占有對象o1,等待對象o2 // flag=0,占有對象o2,等待對象o1 public int flag = 1; // 定義兩個Object對象,模擬兩個線程占有的資源 public static Object o1 = new Object(); public static Object o2 = new Object(); public static void main(String[] args) { TestDeadLock deadLock1 = new TestDeadLock(); TestDeadLock deadLock2 = new TestDeadLock(); deadLock1.flag = 0; deadLock2.flag = 1; Thread thread1 = new Thread(deadLock1); Thread thread2 = new Thread(deadLock2); thread1.start(); thread2.start(); } @Override public void run() { System.out.println("flag: " + flag); // deadLock2占用資源o1,準備獲取資源o2 if (flag == 1) { synchronized (o1) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (o2) { System.out.println("1"); } } } // deadLock1占用資源o2,準備獲取資源o1 else if (flag == 0) { synchronized (o2) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (o1) { System.out.println("0"); } } } } }
https://www.geeksforgeeks.org/lifecycle-and-states-of-a-thread-in-java/
https://www.jianshu.com/p/8cf78bf94f9d
到此,相信大家對“Java中線程及線程狀態是什么意思”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。