java並發-鎖-synchromized

來源:互聯網
上載者:User

標籤:安全性   exception   代碼   size   rri   靜態   執行   over   系統   

關鍵字synchromized 的作用是實現線程間的同步。它的工作室對同步的代碼加鎖,使得每一,只有一個線程可以進入同步塊,從而保證線程間的安全性。

synchromized 的使用者這裡做一個簡單的整理,

1,指定加鎖對象:對給定對象加鎖,進入同步代碼前要獲得給定對象的鎖。

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

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

如下面一段代碼給制定對象加鎖:

public class Test implements Runnable {    static Object lock = new Object();    static int i = 0;    @Override    public void run() {        for (int j = 0; j < 100000; j++) {            synchronized (lock) {                i++;            }        }    }    public static void main(String args[]) throws InterruptedException {        Test test = new Test();        Thread thread1 = new Thread(test);        Thread thread2 = new Thread(test);        thread1.start();        thread2.start();        thread1.join();        thread2.join();        System.out.println(i);    }}

當然上面代碼也可將synchromized關鍵字作用於執行個體方法上:

public class Test implements Runnable {    static int i = 0;    public synchronized void increase(){        i++;    }    @Override    public void run() {        for (int j = 0; j < 100000; j++) {            increase();        }    }    public static void main(String args[]) throws InterruptedException {        Test test = new Test();        Thread thread1 = new Thread(test);        Thread thread2 = new Thread(test);        thread1.start();        thread2.start();        thread1.join();        thread2.join();        System.out.println(i);    }}

同時這裡一定要注意的是Thread的建立方式,這裡Test類只new一個執行個體 ,因為synchronized 直接作用於執行個體方法,需要線程指向同一個執行個體,才能保證多線程使用的是同一個鎖。

如果使用了下面的方式,那麼就會產生兩個Test執行個體,thread1和thread2也就獲得了不同的鎖,所以synchronized 作用於執行個體方法時,需要保證建立線程是同一個Runnable介面執行個體。

Thread thread1 = new Thread(new Test());Thread thread2 = new Thread(new Test());

當然,使用synchronized 第三種方式將其作用於靜態方法,就沒有問題了

 public static synchronized void increase(){        i++;    }

其他,synchronized的使用個人覺得對對象加鎖的方式是優於對執行個體方法的使用,

當某個線程進入同步方法獲得對象鎖,那麼其他線程訪問這裡對象的同步方法時,必須等待或者阻塞,這對高並發的系統是致命的,這很容易導致系統的崩潰。如果某個線程在同步方法裡面發生了死迴圈,那麼它就永遠不會釋放這個對象鎖,那麼其他線程就要永遠的等待。

下面是jdk對hash的實現方式,可以看到,很多方法都是使用了 synchronized 進行了修飾,那麼就意味如果還有別的同步方法x,這個x方法和get方法即使在沒有衝突的情況下也需要等待執行(比如有3個線程同時做讀操作,實際上是不需要阻塞,可以同時進行的)。這樣效率上 必然會有一定的影響,所以會有 ConcurrentHashMap 進行分段加鎖

public synchronized V get(Object key) {        Entry<?,?> tab[] = table;        int hash = key.hashCode();        int index = (hash & 0x7FFFFFFF) % tab.length;        for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {            if ((e.hash == hash) && e.key.equals(key)) {                return (V)e.value;            }        }        return null;    }public synchronized V put(K key, V value);public synchronized V remove(Object key)

 

java並發-鎖-synchromized

聯繫我們

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