Java並發編程原理與實戰十四:Lock介面的認識和使用

來源:互聯網
上載者:User

標籤:nts   current   atom   無法   ext   tran   syn   實戰   volatile   

保證安全執行緒演化:

synchronized

volatile

AtomicInteger

 

Lock介面提供的方法:

void lock():加鎖

void unlock():解鎖

void lockInterruptibly():在多個線程並發擷取鎖的時候,那麼,當有些線程拿不到鎖時會等待,會不停的去搶佔資源,在搶佔的過程中,使用synchronized是無法進行中斷的,那麼,使用lockInterruptibly()方法,如果在搶佔過程中發出一個中斷,是可以中斷的。

tryLock():非阻塞的擷取鎖,如果擷取鎖成功,返回true;否則,返回false

 

package com.roocon.thread.t9;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class Sequence {    private int value;    Lock lock = new ReentrantLock();//所有的線程用一把鎖,來保證安全執行緒    public  int getNext() {        lock.lock();        int a = value ++;        lock.unlock();        return a;    }    public static void main(String[] args) {        Sequence s = new Sequence();        new Thread(new Runnable() {            @Override            public void run() {                while(true) {                    System.out.println(Thread.currentThread().getName() + " " + s.getNext());                    try {                        Thread.sleep(100);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        }).start();        new Thread(new Runnable() {            @Override            public void run() {                while(true) {                    System.out.println(Thread.currentThread().getName() + " " + s.getNext());                    try {                        Thread.sleep(100);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        }).start();        new Thread(new Runnable() {            @Override            public void run() {                while(true) {                    System.out.println(Thread.currentThread().getName() + " " + s.getNext());                    try {                        Thread.sleep(100);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        }).start();    }}

運行結果:

Thread-0 0Thread-2 2Thread-1 1Thread-0 3Thread-1 5Thread-2 4Thread-0 6Thread-1 7Thread-2 8...

 

Lock需要顯示的擷取鎖和釋放鎖,繁瑣能讓代碼更靈活;

使用Lock可以方便的實現公平性;public ReentrantLock(boolean fair){...}

非阻塞的擷取鎖;

能被中斷的擷取鎖;

逾時擷取鎖;(在一定時間內去擷取鎖)

 

synchronized不需要顯示的擷取鎖和釋放鎖,簡單

 

參考資料:

《javab並發編程與實戰》 龍果學院

Java並發編程原理與實戰十四:Lock介面的認識和使用

聯繫我們

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