Java volatile 的測試(Java程式碼實戰-004)

FrankYou發表於2018-05-21

 

package Threads;

/**
 * Created by xfyou 2018/5/21 16:07.
 */
public class VolatileTest {

    private static volatile int MY_INT = 0;

    /**
     * 主執行緒啟動2個測試子執行緒
     *
     * @param args null
     */
    public static void main(String[] args) {
        new ChangeListener().start();
        new ChangeMaker().start();
    }

    /**
     * 此執行緒負責對MY_INT值改變的偵聽,如果有改變就會列印出來
     */
    static class ChangeListener extends Thread {
        @Override
        public void run() {
            int local_value = MY_INT;
            while (local_value < 5) {
                if (local_value != MY_INT) {
                    System.out.println("Got Change for MY_INT : " + MY_INT);
                    local_value = MY_INT;
                }
            }
        }
    }

    /**
     * 此執行緒負責改變MY_INT的值
     */
    static class ChangeMaker extends Thread {
        @Override
        public void run() {
            int local_value = MY_INT;
            while (MY_INT < 5) {
                System.out.println("Incrementing MY_INT to " + (local_value + 1));
                MY_INT = ++local_value;
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

當使用volatile時,修改執行緒對static MY_INT值的修改,另一個偵聽執行緒(讀MY_INT的值)是可以及時讀取到的,輸出結果如下:

Incrementing MY_INT to 1
Got Change for MY_INT : 1
Incrementing MY_INT to 2
Got Change for MY_INT : 2
Incrementing MY_INT to 3
Got Change for MY_INT : 3
Incrementing MY_INT to 4
Got Change for MY_INT : 4
Incrementing MY_INT to 5
Got Change for MY_INT : 5

當不使用volatile時,修改執行緒對static MY_INT值的修改,另一個偵聽執行緒(讀MY_INT的值)可能完全讀取不到(無感知),可能的輸出結果如下:

Incrementing MY_INT to 1
Incrementing MY_INT to 2
Incrementing MY_INT to 3
Incrementing MY_INT to 4
Incrementing MY_INT to 5

 

相關文章