Java多線程基礎(三)Java傳統線程互斥技術

jopen 8年前發布 | 8K 次閱讀 Java開發

Java多線程基礎(三)Java傳統線程互斥技術

java的線程互斥主要通過synchronized關鍵字實現。下面的示例代碼展示了幾種使用synchronized關鍵字的基本用法。

package cn.king;

public class TraditionalThreadSynchronized {

    public static void main(String[] args) {
        new TraditionalThreadSynchronized().foo();
    }

    private void foo() {
        // printer須是final的,否則無法編譯。這主要是為了保證printer的一致性。
        final Printer printer = new Printer();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    /* * Cannot refer to a non-final variable printer * inside an inner class defined in a different method * 更多內容可參閱java8 lambda表達式(閉包)相關知識 */
                    printer.output("123456789");
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    printer.output("abcdefghi");
                }
            }
        }).start();
    }

    static class Printer {
        String _lock = "";
        public void output(String name) {
            int len = name.length();
            // 同步代碼塊

            /* 方法1: * 以this作為鎖對象, * 與使用this加鎖的代碼塊或synchronized方法互斥 */
            // synchronized(this) {

            /* 方法2: * 以Outputer類的字節碼對象(該對象由虛擬機自動創建)作為鎖對象, * 與使用Outputer.class加鎖的代碼塊或static synchronized方法互斥 */
            // synchronized(Outputer.class) {

            /* 方法3: * 以自定義對象作為鎖對象, * 與使用_lock加鎖的代碼塊互斥 */
            synchronized(_lock) {
                for(int i=0; i<len; i++) {
                    System.out.print(name.charAt(i));
                }
                System.out.println();
            }
        }

        // 同步方法,相當于synchronized(this){}
        public synchronized void output2(String name) {
            int len = name.length();
            for(int i=0; i<len; i++) {
                System.out.print(name.charAt(i));
            }
            System.out.println();
        }

        // 靜態同步方法,相當于synchronized(Outputer.class){}
        public static synchronized void output3(String name) {
            int len = name.length();
            for(int i=0; i<len; i++) {
                System.out.print(name.charAt(i));
            }
            System.out.println();
        }
    }
}

上面的代碼中展示了三種基本的線程互斥實現。下面詳述三種方法的實現特點和適用情況。

  1. 以this作為鎖對象
    實現方式
    synchronized(this){…}
    synchronized實例方法
    適用情況
    適用于使用類的同一個實例(對象)作為鎖對象。下面情況不適用:
    若上述代碼中第26行和第39行,改為
    new Printer().output(“<相應字符串>”);
    則無法使用本方法實現線程互斥,而須采用第2種方法。

  2. 以Outputer.class作為鎖對象
    Outputer類的字節碼對象由jvm自動加載。
    實現方式
    synchronized(Outputer.class){…}
    static synchronized方法
    適用情況
    適用于整個類的所有對象都需要互斥訪問的情況。

  3. 以自定義的對象作為所對象
    實現方式
    synchronized(<自定義鎖對象>){…}
    適用情況
    同一個類中代碼塊或者方法的互斥,一般可以用第1種和第2種方法替換。當出現需要在多個類(或者多個類的實例)之間進行互斥控制時,可能需要采用本方法。

來自: http://blog.csdn.net//kingzone_2008/article/details/48166731

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!