具有ReadWriteLock的Java并发
在Java中, sync關(guān)鍵字用于獲取對(duì)象的排他鎖。 當(dāng)線程獲取對(duì)象的鎖以進(jìn)行讀取或?qū)懭霑r(shí),其他線程必須等待,直到該對(duì)象的鎖被釋放。 想想一個(gè)場(chǎng)景,有許多讀取器線程經(jīng)常讀取共享數(shù)據(jù),而只有一個(gè)寫入器線程更新共享數(shù)據(jù)。 讀取時(shí)不必排他地鎖定對(duì)共享數(shù)據(jù)的訪問,因?yàn)槌怯袑懭氩僮?#xff0c;否則可以并行執(zhí)行多個(gè)讀取操作。
在本文中,我將提供Java 1.5 API文檔中引入的ReadWriteLock接口的示例用法。 在Java Api文檔中,它說:
ReadWriteLock維護(hù)一對(duì)關(guān)聯(lián)的鎖,
一種用于只讀操作,另一種用于寫操作。
讀取鎖可以由多個(gè)讀取器線程同時(shí)保持,
只要沒有作家。 寫鎖是排他的。
讀取器線程可以同時(shí)讀取共享數(shù)據(jù)。 讀取操作不會(huì)阻止其他讀取操作。 執(zhí)行SQL SELECT語(yǔ)句時(shí)就是這種情況。 但是寫操作是排他的。 這意味著當(dāng)寫入器線程持有用于修改共享數(shù)據(jù)的鎖時(shí),所有讀取器和其他寫入器均被阻止。
Writer.java此類表示更新共享數(shù)據(jù)的線程。 Writer使用ReadWriteLock的WriteLock專門鎖定對(duì)字典的訪問。
package deneme.readwritelock;public class Writer extends Thread{private boolean runForestRun = true;private Dictionary dictionary = null;public Writer(Dictionary d, String threadName) {this.dictionary = d;this.setName(threadName);}@Overridepublic void run() {while (this.runForestRun) { String [] keys = dictionary.getKeys();for (String key : keys) {String newValue = getNewValueFromDatastore(key);//updating dictionary with WRITE LOCKdictionary.set(key, newValue);}//update every secondstry {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}public void stopWriter(){this.runForestRun = false;this.interrupt();}public String getNewValueFromDatastore(String key){//This part is not implemented. Out of scope of this artilereturn "newValue";}}Reader.java此類表示讀取共享數(shù)據(jù)的線程。
package deneme.readwritelock;public class Reader extends Thread{private Dictionary dictionary = null;public Reader(Dictionary d, String threadName) {this.dictionary = d;this.setName(threadName);}private boolean runForestRun = true;@Overridepublic void run() {while (runForestRun) {String [] keys = dictionary.getKeys();for (String key : keys) {//reading from dictionary with READ LOCKString value = dictionary.get(key);//make what ever you want with the value.System.out.println(key + " : " + value);}//update every secondstry {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}public void stopReader(){this.runForestRun = false;this.interrupt();}}Dictionary.java這是一個(gè)簡(jiǎn)單且線程安全的字典。 讀操作通過ReadLock進(jìn)行管理,寫操作(更新)通過WriteLock進(jìn)行管理。
package deneme.readwritelock;import java.util.HashMap;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantReadWriteLock;public class Dictionary {private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();private final Lock read = readWriteLock.readLock();private final Lock write = readWriteLock.writeLock();private HashMap<String, String> dictionary = new HashMap<String, String>();public void set(String key, String value) {write.lock();try {dictionary.put(key, value);} finally {write.unlock();}}public String get(String key) {read.lock();try{return dictionary.get(key);} finally {read.unlock();}}public String[] getKeys(){read.lock();try{String keys[] = new String[dictionary.size()];return dictionary.keySet().toArray(keys);} finally {read.unlock();}}public static void main(String[] args) {Dictionary dictionary = new Dictionary();dictionary.set("java", "object oriented");dictionary.set("linux", "rulez");Writer writer = new Writer(dictionary, "Mr. Writer");Reader reader1 = new Reader(dictionary ,"Mrs Reader 1");Reader reader2 = new Reader(dictionary ,"Mrs Reader 2");Reader reader3 = new Reader(dictionary ,"Mrs Reader 3");Reader reader4 = new Reader(dictionary ,"Mrs Reader 4");Reader reader5 = new Reader(dictionary ,"Mrs Reader 5");writer.start();reader1.start();reader2.start();reader3.start();reader4.start();reader5.start();}}參考:來(lái)自我們的JCG合作伙伴 Ilkin Ulas的Java中的ReadWriteLock示例, 您的所有博客都屬于我們博客。
翻譯自: https://www.javacodegeeks.com/2012/04/java-concurrency-with-readwritelock.html
總結(jié)
以上是生活随笔為你收集整理的具有ReadWriteLock的Java并发的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 凡科域名备案有什么用(凡科域名备案)
- 下一篇: 魅族 安卓(安卓n魅族)