java中synchronized的關鍵字

來源:互聯網
上載者:User

標籤:override   except   bsp   cep   stat   start   run   tar   ring   

java中每個對象都會有一個對象鎖,而synchronized就是得到這個鎖,看下面這個例子

import java.util.Random;public class MyData{        public synchronized void increment() {        for (int i = 0; i < 10; i++) {            try {                Thread.sleep(new Random().nextInt(200));            } catch (Exception e) {                e.printStackTrace();            }           System.out.println(Thread.currentThread().getName() + ":" +i);        }    }        public synchronized void decrement() {        for (int i = 0; i < 10; i++) {            try {                Thread.sleep(new Random().nextInt(200));            } catch (Exception e) {                e.printStackTrace();            }            System.out.println(Thread.currentThread().getName() + ":" +i);        }    }        public static void main(String[] args) {        final MyData myData1 = new MyData();       // final MyData myData2 = new MyData();       new Thread(new Runnable() {            @Override            public void run() {                myData1.increment();            }        }).start();                new Thread(new Runnable() {            @Override            public void run() {                myData1.decrement();            }        }).start();    }}

 

無論執行多少次都是有序的,兩個線程操作的是同一個對象,第一個執行的線程得到了鎖,第二個線程只能等第一個線程執行完了才能拿到鎖,進入方法。

再看下面這個例子

import java.util.Random;public class MyData{        public synchronized void increment() {        for (int i = 0; i < 10; i++) {            try {                Thread.sleep(new Random().nextInt(200));            } catch (Exception e) {                e.printStackTrace();            }           System.out.println(Thread.currentThread().getName() + ":" +i);        }    }        public synchronized void decrement() {        for (int i = 0; i < 10; i++) {            try {                Thread.sleep(new Random().nextInt(200));            } catch (Exception e) {                e.printStackTrace();            }            System.out.println(Thread.currentThread().getName() + ":" +i);        }    }        public static void main(String[] args) {        final MyData myData1 = new MyData();        final MyData myData2 = new MyData();       new Thread(new Runnable() {            @Override            public void run() {                myData1.increment();            }        }).start();                new Thread(new Runnable() {            @Override            public void run() {                myData2.decrement();            }        }).start();    }}

執行的結果是無序的,兩個對象,兩把鎖,故互不影響,各自執行各自的。

再來看看下面這個例子

import java.util.Random;
public class MyData{
    
    public synchronized void increment() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(new Random().nextInt(200));
            } catch (Exception e) {
                e.printStackTrace();
            }
           System.out.println(Thread.currentThread().getName() + ":" +i);
        }
    }
    
    public static synchronized void decrement() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(new Random().nextInt(200));
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ":" +i);
        }
    }
    
    public static void main(String[] args) {
        final MyData myData1 = new MyData();
      //  final MyData myData2 = new MyData();
       new Thread(new Runnable() {
            @Override
            public void run() {
                myData1.increment();
            }
        }).start();
        
        new Thread(new Runnable() {
            @Override
            public void run() {
                MyData.decrement();
            }
        }).start();
    }
}

結果也是無序的,原因和上面一樣,static方法是屬於Class對象的,故decrement方法鎖的MyData.Class對象,而myData1.increment();鎖的是myData1對象,互不干擾。

只需記住synchronized鎖的是對象,每個對象有一把對象鎖,拿到鎖之後才能執行synchronized的方法

java中synchronized的關鍵字

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.