1. 程式人生 > >Java執行狀態分析1:執行緒及執行緒狀態

Java執行狀態分析1:執行緒及執行緒狀態

執行緒

執行緒(英語:thread)是作業系統能夠進行運算排程的最小單位。它被包含在程序之中,是程序中的實際運作單位。一條執行緒指的是程序中一個單一順序的控制流,
一個程序中可以併發多個執行緒,每條執行緒並行執行不同的任務。在Unix System V及SunOS中也被稱為輕量程序(lightweight processes),
但輕量程序更多指核心執行緒(kernel thread),而把使用者執行緒(user thread)稱為執行緒。

以上拷貝自維基百科

程式碼中任務、邏輯操作都依賴於執行緒,是java執行時最寶貴的資源

多執行緒一定程度可以增加cpu使用時間,壓榨計算機資源提供更好的使用效能,一定程度也增加了資源的消耗如記憶體的增長、執行緒上下文資料切換的消耗、cup資源消耗,實際情況中我們應該根據業務場景合理的使用執行緒資源

Java執行緒生命週期

![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;
  1. New:剛建立,可被執行,並且未開始執行
  2. Runnable:正在執行或隨時準備執行,例如多執行緒程式分配特定時間片給特定執行緒,特定執行緒執行短暫時間並暫停放棄cpu時間給其他執行緒,其他執行緒因此可以執行,這種場景執行緒是準備執行等待CPU時間,這種狀態即Runnable
  3. Blocked:waiting for a monitor lock,處於需要獲取其他執行緒鎖定的同步資源,如等待io結束,這種狀態在轉變為Runnable之前無法執行,無法消耗cup時間片
  4. Waiting:等待其他執行緒執行特定操作,和Blocked類似
  5. Timed Waiting:執行緒呼叫等待執行場景,特定時間後執行,比較sleep,或者一些條件等待場景,如定時任務
  6. 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 - " + thread1.getState());
        thread1.start();

        // thread1 呼叫start後 變成 Runnable state
        System.out.println("State of thread1 after calling .start() method on it - " +
                thread1.getState());

    }

}

class TestThread1 implements Runnable {

    @Override
    public void run() {

        TestThread2 myThread = new TestThread2();
        Thread thread2 = new Thread(myThread);

        // 執行緒2建立 NEW state.
        System.out.println("State of thread2 after creating it - " + thread2.getState());
        thread2.start();

        // 執行緒2呼叫start 變成 Runnable state
        System.out.println("State of thread2 after calling .start() method on it - " +
                thread2.getState());

        // 呼叫sleep迫使當前執行緒進入sleep thread2 = 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 {
            // 呼叫join迫使執行緒結束到die
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("State of thread2 when it has finished it's execution - " +
                thread2.getState());
    }
}


class TestThread2 implements Runnable {

    @Override
    public void run() {
        // moving thread2 to timed waiting state
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("State of thread1 while it called join() method on thread2 -" +
                DemonstrateThreadStates2.thread1.getState());
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

控制檯輸出:

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.c