標籤:線程 lock 讀寫鎖
在前面我們在解決線程同步問題的時候使用了synchronized關鍵字,今天我們來看看Java 5.0以後提供的線程鎖Lock.
Lock介面的實作類別提供了比使用synchronized關鍵字更加靈活和廣泛的鎖定對象操作,而且是以物件導向的方式進行對象加鎖。
@Overridepublic void run() {while(true){Lock lock = new ReentrantLock();try {lock.lock();Thread.sleep(new Random().nextInt(3000));String data = readData();System.out.print("讀取資料: " + data);} catch (InterruptedException e) {e.printStackTrace();}finally{lock.unlock();}}}
讀寫鎖:分為讀鎖和寫鎖,多個讀鎖不互斥,讀鎖與寫鎖互斥,寫鎖與寫鎖互斥,這是由JVM控制的。
import java.util.Random;import java.util.concurrent.locks.ReadWriteLock;import java.util.concurrent.locks.ReentrantReadWriteLock;public class ReadWriteLockTest {static ReadWriteLock rwl = new ReentrantReadWriteLock();private static String data = null;public static void main(String[] args) {Runnable runnable1 = new MyRunnable1();Runnable runnable2 = new MyRunnable2();for(int i=0; i<3; i++){new Thread(runnable1).start();new Thread(runnable2).start();}}static class MyRunnable1 implements Runnable{@Overridepublic void run() {writeData("" + new Random().nextInt(100));}}static class MyRunnable2 implements Runnable{@Overridepublic void run() {readData();}}private static void writeData(String var){rwl.writeLock().lock();try {System.out.println(Thread.currentThread().getName() + " 準備寫");Thread.sleep(new Random().nextInt(3000));data = var;System.out.println(Thread.currentThread().getName() + " 寫完畢");} catch (InterruptedException e) {e.printStackTrace();}finally{rwl.writeLock().unlock();}}private static void readData(){rwl.readLock().lock(); //用讀鎖鎖住try {System.out.println(Thread.currentThread().getName() + " 準備讀");Thread.sleep(new Random().nextInt(3000));System.out.println(Thread.currentThread().getName() + " 讀完畢");} catch (InterruptedException e) {e.printStackTrace();}finally{rwl.readLock().unlock();}}}
用過Hibernate架構的朋友可能知道,Hibernate查詢資料庫有緩衝機制,如果某個資料在記憶體中存在則可以並發的去讀取,如果緩衝中沒有資料則需要互斥的去從資料庫取資料。
import java.util.HashMap;import java.util.Map;import java.util.concurrent.locks.ReentrantReadWriteLock;public class CacheDemo {private Map<String, Object> cache = new HashMap<String, Object>();public static void main(String[] args) {}private ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();/** * 實現多個並發讀,互斥的寫 * @param key * @return */public Object getData(String key){rwl.readLock().lock();Object value = null;try{value = cache.get(key);if(value == null){rwl.readLock().unlock(); //釋放讀鎖rwl.writeLock().lock(); //添加寫鎖try{if(value == null){ //放置其他線程載入資料value = "去資料庫查詢"; //這裡類比從資料庫查詢if(value == null){//TODO 拋出異常}}}finally{rwl.writeLock().unlock();}rwl.readLock().lock(); //鎖還給讀線程}}finally{rwl.readLock().unlock();}return value;}}上面擷取資料的大概過程如下:
1、擷取讀鎖,讀取資料
2、如果有資料則直接返回,並釋放讀鎖讓其他線程讀。
3、如果記憶體中沒有資料則從資料庫寫入記憶體,釋放讀鎖並添加寫鎖(這樣寫入資料就可以達到可以互斥)
4、讀入記憶體後釋放寫鎖並還回讀鎖(和後面的unlock()對應)
5、如果在添加寫鎖的時候同時有多個線程,則只能有其中一個線程搶到鎖,等擁有鎖的線程釋放寫鎖後,其他線程就會搶到寫鎖,但是此時資料已經寫入記憶體,則需要判斷記憶體資料是否為null如果不為null則直接釋放寫鎖。