Java線程同步操作

來源:互聯網
上載者:User

標籤:++   pre   pos   new t   oid   code   count   tin   thread   

synchronized

作用於對象執行個體:對給定對象加鎖,進入同步代碼前要獲得給定對象的鎖。

作用於執行個體方法:相當於對當前執行個體加鎖,進入同步代碼前要獲得當前執行個體的鎖。

作用於靜態方法:相當於對當前類加鎖,進入同步代碼前要獲得當前類的鎖。

使用給執行個體對象加鎖
public class AccountingSync implements Runnable {    static AccountingSync instance = new AccountingSync();    static int i = 0;    @Override    public void run() {        for (int k = 0; k < 10000; k++) {            synchronized (instance) {                i++;            }        }    }    @Test    public void testInteger() throws InterruptedException {        int count = 10;        Thread[] ts = new Thread[count];        for (int k = 0; k < count; k++) {            ts[k] = new Thread(instance);        }        // start        for (int k = 0; k < count; k++) {            ts[k].start();        }        // join        for (int k = 0; k < count; k++) {            ts[k].join();        }        System.out.println(i);    }}
給類方法加鎖
public class AccountingSync2 implements Runnable {    static AccountingSync2 instance = new AccountingSync2();    static int i = 0;    public synchronized void increase() {        i++;    }    @Override    public void run() {        for (int k = 0; k < 10000; k++) {            increase();        }    }    @Test    public void testInteger() throws InterruptedException {        int count = 10;        Thread[] ts = new Thread[count];        for (int k = 0; k < count; k++) {            ts[k] = new Thread(instance);        }        // start        for (int k = 0; k < count; k++) {            ts[k].start();        }        // join        for (int k = 0; k < count; k++) {            ts[k].join();        }        System.out.println(i);    }}
給類方法加鎖的錯誤示範
public class AccountingSyncBad implements Runnable {    static int i = 0;    public synchronized void increase() {        i++;    }    @Override    public void run() {        for (int k = 0; k < 10000; k++) {            increase();        }    }    @Test    public void testInteger() throws InterruptedException {        int count = 10;        Thread[] ts = new Thread[count];        for (int k = 0; k < count; k++) {            ts[k] = new Thread(new AccountingSyncBad());        }        // start        for (int k = 0; k < count; k++) {            ts[k].start();        }        // join        for (int k = 0; k < count; k++) {            ts[k].join();        }        System.out.println(i);    }}

假設把給類執行個體加鎖中的每個執行個體比作一個門,上面的測試方法中每個門都有鎖但是10個門10把鎖,每個線程進一個門。還是不能保證臨界區資源i同時只一個線程訪問

fix

@Testpublic void testIntegerFix() throws InterruptedException {  int count = 10;  AccountingSyncBad instance = new AccountingSyncBad();  Thread[] ts = new Thread[count];  for (int k = 0; k < count; k++) {    ts[k] = new Thread(instance);  }  // start  for (int k = 0; k < count; k++) {    ts[k].start();  }  // join  for (int k = 0; k < count; k++) {    ts[k].join();  }  System.out.println(i);}
給靜態類方法加鎖
public class AccountingSyncClass implements Runnable {    static int i = 0;    public static synchronized void increase() {        i++;    }    @Override    public void run() {        for (int k = 0; k < 10000; k++) {            increase();        }    }    @Test    public void testInteger() throws InterruptedException {        int count = 10;        Thread[] ts = new Thread[count];        for (int k = 0; k < count; k++) {            ts[k] = new Thread(new AccountingSyncClass());        }        // start        for (int k = 0; k < count; k++) {            ts[k].start();        }        // join        for (int k = 0; k < count; k++) {            ts[k].join();        }        System.out.println(i);    }        @Test    public void testIntegerFix() throws InterruptedException {        int count = 10;        AccountingSyncClass instance = new AccountingSyncClass();        Thread[] ts = new Thread[count];        for (int k = 0; k < count; k++) {            ts[k] = new Thread(instance);        }        // start        for (int k = 0; k < count; k++) {            ts[k].start();        }        // join        for (int k = 0; k < count; k++) {            ts[k].join();        }        System.out.println(i);    }}

上面測試的testInteger方法和testIntegerFix方法都能得到正確的結果,原因是給靜態類方法加鎖相當於10個門用的同一把鎖,保證了同一時間只有一個線程能訪問臨界區資源i。

Java線程同步操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.