NO.36 璇诲啓鍒嗙:CopyOnWrite鏈哄埗
Posted 绋嬪簭鍛榗hatbook
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NO.36 璇诲啓鍒嗙:CopyOnWrite鏈哄埗相关的知识,希望对你有一定的参考价值。
鍔犲叆璇讳功璺佃缇わ紝姣忓ぉ涓€涓煡璇嗙偣锛屾寔缁簿杩涳紒
杩欐槸绋嬪簭鍛榗hatbook绗?span>90绡囧師鍒?/span>
浠婃棩闅惧害绯绘暟 锛氿煂燄煂?/p>
棰勮闃呰鏃堕棿 锛?nbsp;5 鍒嗛挓
鈻?/span>
00銆佽瘽棰樼殑鐢辨潵
鍦ㄧ34绡嘕ava绯诲垪鏂囩珷涓紙锛夛紝鎴戜滑璁插埌浜嗗寘鎷?span class="mq-50">CopyOnWriteArraySet銆丆opyOnWriteArrayList鍦ㄥ唴鐨?/span>绾跨▼瀹夊叏鐨勬暟鎹粨鏋勩€傚叾涓紝鏈夐儴鍒嗚€侀搧鍦ㄥ悗鍙扮暀瑷€涓彁鍒颁簡涓轰粈涔堜笂杩颁袱涓暟鎹粨鏋勯兘鏈変竴涓€滃鎬€濈殑鍓嶇紑鈥斺€?span class="mq-51">CopyOnWrite锛?/span>
鏈汉涔熻寰楄繖鏄竴涓緢鏈夎叮鐨勮瘽棰橈紝浠婂ぉ鎴戜滑涓€璧锋潵鎵掍竴鎵掕繖涓湁鍐呮兜鐨勬暟鎹粨鏋勭殑鈥滃畾璇€濄€?/span>
01銆?span class="mq-61">CopyOnWrite鍘熺悊
CopyOnWrite鏈哄埗绠€鍗曟潵璇村氨鏄€滃啓鏃跺鍒垛€濇€濇兂銆傚嵆锛氬綋鎴戜滑瀵逛竴涓鍣ㄨ繘琛屽啓鎿嶄綔鏃讹紙add銆乺emove绛夛級锛屼笉鏄洿鎺ュ線褰撳墠瀹瑰櫒鍐欏唴瀹癸紝鑰屾槸鍏堝皢褰撳墠瀹瑰櫒澶嶅埗涓€浠斤紝褰㈡垚涓€涓柊瀹瑰櫒锛岀劧鍚庡皢瑕佸啓鐨勫唴瀹瑰啓鍏ユ柊瀹瑰櫒涓紝褰撳啓鎿嶄綔瀹屾垚鍚庯紝鍐嶇敤鏂板鍣ㄧ殑寮曠敤鍘绘洿鏂板師瀹瑰櫒鐨勫紩鐢ㄣ€?/p>
閫氳繃涓婅堪鍐呭锛屾垜浠彲浠ユ帹鏂嚭鍩轰簬CopyOnWrite鍘熺悊瀹炵幇鐨勫鍣細
鍙互鍦ㄤ笉鍔犻攣鐨勬儏鍐典笅锛屾敮鎸佸涓苟鍙戠殑璇绘搷浣滐紱褰撶劧锛屽鏋滃湪杩涜鍐欐搷浣滄椂锛屼笉瀵瑰師瀹瑰櫒鍔犻攣鐨勮瘽锛屾湁鍙兘璇诲埌涓嶆槸鏈€鏂扮殑鏁版嵁锛涗絾鏄紝濡傛灉鍔犻攣灏嗗鑷存棤娉曞苟鍙戣銆?/p>
鍦ㄨ繘琛屽苟鍙戝啓鎿嶄綔鏃讹紝鏄渶瑕佸姞閿佺殑锛涘鏋滀笉鍔犻攣锛屼細鍑虹幇鏁版嵁閬楁紡鎯呭喌
璇绘搷浣滄槸浣滅敤浜庡師瀹瑰櫒涓婏紱鍐欐搷浣滄槸浣滅敤浜庡鍒跺悗褰㈡垚鐨勬柊瀹瑰櫒涓婏紱璇讳笌鍐欎綔鐢ㄤ簬涓嶅悓鐨勫鍣紝瀹炶川涓婃槸浣撶幇浜嗕竴绉嶈鍐欏垎绂荤殑鎬濇兂
02銆?span>CopyOnWrite搴旂敤
JDK鎻愪緵浜嗗熀浜嶤opyOnWrite鏈哄埗鐨?span class="mq-76">CopyOnWriteArraySet銆丆opyOnWriteArrayList鏁版嵁缁撴瀯锛涗笅闈㈡垜浠埄鐢ㄤ笂杩版満鍒舵潵瀹炵幇涓€涓狢opyOnWriteHashMap銆備负浜嗗敖鍙兘鐨勮鏄庡叾瀹炵幇鍘熺悊锛屽苟娌℃湁鎶奙ap鏀寔鐨勬墍鏈夋搷浣滆繘琛屽疄鐜帮紝鑰屽彧鏄疄鐜颁簡鍏稿瀷鐨刾ut銆乬et鏂规硶锛屽叿浣撳疄鐜拌瑙佷唬鐮?銆?/span>
1
public class CopyOnWriteHashMap<K,V> implements Map<K,V>, Cloneable{
private volatile Map<K,V> dataMap;
final Lock lock = new ReentrantLock();
public CopyOnWriteHashMap(){
dataMap = new HashMap<K,V>();
}
//鍐欐搷浣滆鍔犻攣
public V put(K key, V value){
// 鑾峰彇閿?/span>
final Lock lock = this.lock;
lock.lock();
try {
Map<K,V> newMap = new HashMap<K,V>(dataMap);
V val = newMap.put(key, value);
this.dataMap = newMap;
return val;
} finally {
// 閲婃斁閿?/span>
lock.unlock();
}
}
//璇绘搷浣滀笉鍔犻攣
public V get(Object key){
return this.dataMap.get(key);
}
//鍐欐搷浣滆鍔犻攣
public void putAll(Map<? extends K, ? extends V> newData){
// 鑾峰彇閿?/span>
final Lock lock = this.lock;
lock.lock();
try {
Map<K,V> newMap = new HashMap<K,V>(this.dataMap);
newMap.putAll(newData);
this.dataMap = newMap;
} finally {
// 閲婃斁閿?/span>
lock.unlock();
}
}
//TODO
}
鑰侀搧浠彲浠ョ粏缁嗗搧鍛充唬鐮?锛屾垜浠彲浠ョ户缁妇涓€鍙嶄笁锛屽疄鐜版洿澶氱殑绾跨▼瀹夊叏鐨凜opyOnWrite瀹瑰櫒銆?/p>
03銆?span>CopyOnWrite鐨勫簲鐢ㄥ満鏅?/strong>
鏍规嵁鎴戜滑瀵笴opyOnWrite鐨勬満鍒剁殑鐞嗚В锛孋opyOnWrite骞跺彂瀹瑰櫒鐢ㄤ簬璇诲鍐欏皯鐨勫苟鍙戝満鏅紝鍗筹細淇℃伅寰堝皯琚洿鏂颁絾鏄父琚闂殑涓氬姟鍦烘櫙銆?/p>
姣斿锛屽浜庡徃鏈虹殑甯歌窇绾胯矾鎿嶄綔锛涘綋鐒朵篃鍙敤浜庢垜浠殑閾惰鍗¤处鍙风鐞嗙瓑锛? )銆?/p>
CopyOnWrite瀹瑰櫒瀹炵幇璧锋潵寰堢畝鍗曪紝浣嗘槸鍏剁己鐐瑰悓鏍锋槑鏄撅細
鍐呭瓨鍗犵敤闂鍙婂叾GC甯︽潵鐨勬€ц兘寮€閿€銆傚洜涓篊opyOnWrite閲囩敤鐨勬槸鍐欐椂澶嶅埗锛屽綋鎵ц鍐欐搷浣滅殑鏃跺€欙紝閮戒細澶嶅埗涓€浠藉師瀵硅薄锛屽舰鎴愭柊瀵硅薄锛涘鏋滃師瀵硅薄鍗犵敤鍐呭瓨寰堝ぇ锛堟瘮濡備互G涓哄崟浣嶏級锛岄偅涔堝唴瀛樺崰鐢ㄥ氨浼氶潪甯稿ぇ锛屽悓鏃跺緢鏈夊彲鑳介€犳垚JVM鐨勯绻丟C锛涙澶栵紝鏂板璞℃搷浣滃畬鍚庯紝浼氬皢瀵硅薄鐨勫紩鐢ㄨ祴鍊肩粰鍘熷璞★紝閭d箞鍘熷璞″氨娌℃湁鎸囧悜浜嗭紝濡傛灉棰戠箒鎵ц鍐欐搷浣滐紝灏变細褰㈡垚璁稿杩欐牱鐨勫師瀵硅薄锛屽悓鏍蜂細閫犳垚GC鍥炴敹杩欎簺瀵硅薄鏃舵墍甯︽潵鐨勬€ц兘寮€閿€銆傝繖灏辨槸CopyOnWrite瀹瑰櫒閫傚悎璇诲鍐欏皯鐨勭湡姝e師鍥犮€傞拡瀵瑰唴瀛樺崰鐢ㄩ棶棰橈紝鍙互閫氳繃鍘嬬缉瀹瑰櫒涓殑鍏冪礌鐨勬柟娉曟潵鍑忓皯澶у璞$殑鍐呭瓨娑堣€椼€?/p>
鏃犳硶淇濊瘉鏁版嵁瀹炴椂涓€鑷存€с€侰opyOnWrite瀹瑰櫒鍙兘淇濊瘉鏁版嵁鐨勬渶缁堜竴鑷存€э紝涓嶈兘淇濊瘉鏁版嵁鐨勫疄鏃朵竴鑷存€с€傛墍浠ワ紝鍦ㄥ啓鍏ョ殑鏁版嵁椹笂灏卞笇鏈涜兘璇诲埌鐨勪笟鍔″満鏅腑锛屽嬁浣跨敤CopyOnWrite瀹瑰櫒銆?/p>
鍏充簬涓婅堪鐨勭2鐐圭己鐐癸紝娆㈣繋鑰侀搧浠皢浣犺涓虹殑鍘熷洜鍐欏湪鐣欒█鍖轰笌澶у涓€璧疯璁恒€?/span>
濡傛灉瑙夊緱鏂囩珷鏈夌敤锛屾劅璋㈣€侀搧浠浆鍙戝垎浜紝璁╂洿澶氱殑灏忎紮浼村缓绔嬭繛鎺ワ紒
銆愬弬鑰冭祫鏂欍€?/span>
1 Java澶氱嚎绋嬬紪绋嬪疄鎴樻寚鍗楋紝 榛勬枃娴凤紝 涓浗宸ヤ俊鍑虹増闆嗗洟銆?/span>
2 Java骞跺彂缂栫▼浠庡叆闂ㄥ埌绮鹃€氾紝寮犳尟鍗庯紝娓呭崕澶у鍑虹増绀俱€?/span>
绋嬪簭鍛楥hatbook
绋嬪簭鍛橀兘鍏虫敞浜嗭紝鏉ヤ笉鍙婅В閲婏紝闀挎寜鍥剧墖锛屽揩涓婅溅
鏈枃寤朵几闃呰
涓婃枃1锛?/p>
涓婃枃2锛?/p>
鎺ㄨ崘1锛?/p>
鎺ㄨ崘2锛?/p>
以上是关于NO.36 璇诲啓鍒嗙:CopyOnWrite鏈哄埗的主要内容,如果未能解决你的问题,请参考以下文章