2.3.線程的同步和協作_鎖


synchronized是最基本的同步機制,Java5引入了另外一種更強大更靈活的同步機制:Lock接口及其實現類。Lock接口的引入使得同步機制不再侷限於關鍵字,而可以在API一層實現,深遠的影響了Java的併發基礎設施。


相比synchronized,Lock機制有以下優點:

  • 支持更靈活的同步代碼塊結構。使用synchronized關鍵字時,只能在同一個synchronized塊結構中獲取和釋放控制。Lock接口允許實現更復雜的臨界區結構,臨界區的控制和釋放不出現在同一個塊結構中。
  • 相比synchronized關鍵字,Lock接口提供了更多的功能。
  • Lock接口允許分離讀和寫操作,允許多個讀線程和一個寫線程。
  • 相比synchronized關鍵字,Lock接口具有更好的性能。

Lock的獲取和釋放都需要開發人員控制,所以要特別注意鎖的釋放!下面以一個計數器的實現爲例說明Lock接口的使用方法:

public class CountLockDemo {

    public static void main(String[] args){
        Lock lock = new ReentrantLock();
        Counter counter = new Counter(lock);

        System.out.println("main:創建10個線程");
        Thread[] threads = new Thread[10];
        for(int i=0; i<threads.length ;i++){
            threads[i] = new Thread(counter);
        }

        System.out.println("main:啓動10個線程");
        for(int i=0; i<threads.length ;i++){
            threads[i].start();
        }

        System.out.println("main:等待10個線程");
        try{
            for(int i=0; i<threads.length ;i++){
                threads[i].join();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        System.out.println("main:退出");
    }
}

class Counter implements Runnable{
    private long count = 0;
    private Lock lock = null;

    public Counter(Lock lock){
        this.lock = lock;
    }

    @Override
    public void run() {
        lock.lock();
        try{
            count += 1;
            long duration = (long)(Math.random()*1000);
            Thread.sleep(duration);
            System.out.println(Thread.currentThread().getName() + ":" + count);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally{
            lock.unlock();
        }
    }
}

程序運行日誌:
main:創建10個線程
main:啓動10個線程
main:等待10個線程
Thread-0:1
Thread-2:2
Thread-4:3
Thread-6:4
Thread-8:5
Thread-1:6
Thread-7:7
Thread-9:8
Thread-5:9
Thread-3:10
main:退出


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章