NO.36 璇诲啓鍒嗙:CopyOnWrite鏈哄埗

Posted 绋嬪簭鍛榗hatbook

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NO.36 璇诲啓鍒嗙:CopyOnWrite鏈哄埗相关的知识,希望对你有一定的参考价值。


鍔犲叆璇讳功璺佃缇わ紝姣忓ぉ涓€涓煡璇嗙偣锛屾寔缁簿杩涳紒


NO.36 璇诲啓鍒嗙:CopyOnWrite鏈哄埗
纰庣墖鏃堕棿|浣撶郴瀛︿範
NO.36 璇诲啓鍒嗙:CopyOnWrite鏈哄埗

杩欐槸绋嬪簭鍛榗hatbook绗?span>90绡囧師鍒?/span>

浠婂ぉ鏄?/span>
2018骞寸殑绗?nbsp;澶?/span>


浠婃棩闅惧害绯绘暟 锛氿煂燄煂?/p>

棰勮闃呰鏃堕棿 锛?nbsp;5 鍒嗛挓

鈻?/span>


NO.36 璇诲啓鍒嗙:CopyOnWrite鏈哄埗


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鍘熺悊瀹炵幇鐨勫鍣細

  1. 鍙互鍦ㄤ笉鍔犻攣鐨勬儏鍐典笅锛屾敮鎸佸涓苟鍙戠殑璇绘搷浣滐紱褰撶劧锛屽鏋滃湪杩涜鍐欐搷浣滄椂锛屼笉瀵瑰師瀹瑰櫒鍔犻攣鐨勮瘽锛屾湁鍙兘璇诲埌涓嶆槸鏈€鏂扮殑鏁版嵁锛涗絾鏄紝濡傛灉鍔犻攣灏嗗鑷存棤娉曞苟鍙戣銆?/p>

  2. 鍦ㄨ繘琛屽苟鍙戝啓鎿嶄綔鏃讹紝鏄渶瑕佸姞閿佺殑锛涘鏋滀笉鍔犻攣锛屼細鍑虹幇鏁版嵁閬楁紡鎯呭喌

  3. 璇绘搷浣滄槸浣滅敤浜庡師瀹瑰櫒涓婏紱鍐欐搷浣滄槸浣滅敤浜庡鍒跺悗褰㈡垚鐨勬柊瀹瑰櫒涓婏紱璇讳笌鍐欎綔鐢ㄤ簬涓嶅悓鐨勫鍣紝瀹炶川涓婃槸浣撶幇浜嗕竴绉嶈鍐欏垎绂荤殑鎬濇兂


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瀹瑰櫒瀹炵幇璧锋潵寰堢畝鍗曪紝浣嗘槸鍏剁己鐐瑰悓鏍锋槑鏄撅細

  1. 鍐呭瓨鍗犵敤闂鍙婂叾GC甯︽潵鐨勬€ц兘寮€閿€銆傚洜涓篊opyOnWrite閲囩敤鐨勬槸鍐欐椂澶嶅埗锛屽綋鎵ц鍐欐搷浣滅殑鏃跺€欙紝閮戒細澶嶅埗涓€浠藉師瀵硅薄锛屽舰鎴愭柊瀵硅薄锛涘鏋滃師瀵硅薄鍗犵敤鍐呭瓨寰堝ぇ锛堟瘮濡備互G涓哄崟浣嶏級锛岄偅涔堝唴瀛樺崰鐢ㄥ氨浼氶潪甯稿ぇ锛屽悓鏃跺緢鏈夊彲鑳介€犳垚JVM鐨勯绻丟C锛涙澶栵紝鏂板璞℃搷浣滃畬鍚庯紝浼氬皢瀵硅薄鐨勫紩鐢ㄨ祴鍊肩粰鍘熷璞★紝閭d箞鍘熷璞″氨娌℃湁鎸囧悜浜嗭紝濡傛灉棰戠箒鎵ц鍐欐搷浣滐紝灏变細褰㈡垚璁稿杩欐牱鐨勫師瀵硅薄锛屽悓鏍蜂細閫犳垚GC鍥炴敹杩欎簺瀵硅薄鏃舵墍甯︽潵鐨勬€ц兘寮€閿€銆傝繖灏辨槸CopyOnWrite瀹瑰櫒閫傚悎璇诲鍐欏皯鐨勭湡姝e師鍥犮€傞拡瀵瑰唴瀛樺崰鐢ㄩ棶棰橈紝鍙互閫氳繃鍘嬬缉瀹瑰櫒涓殑鍏冪礌鐨勬柟娉曟潵鍑忓皯澶у璞$殑鍐呭瓨娑堣€椼€?/p>

  2. 鏃犳硶淇濊瘉鏁版嵁瀹炴椂涓€鑷存€с€侰opyOnWrite瀹瑰櫒鍙兘淇濊瘉鏁版嵁鐨勬渶缁堜竴鑷存€э紝涓嶈兘淇濊瘉鏁版嵁鐨勫疄鏃朵竴鑷存€с€傛墍浠ワ紝鍦ㄥ啓鍏ョ殑鏁版嵁椹笂灏卞笇鏈涜兘璇诲埌鐨勪笟鍔″満鏅腑锛屽嬁浣跨敤CopyOnWrite瀹瑰櫒銆?/p>


鍏充簬涓婅堪鐨勭2鐐圭己鐐癸紝娆㈣繋鑰侀搧浠皢浣犺涓虹殑鍘熷洜鍐欏湪鐣欒█鍖轰笌澶у涓€璧疯璁恒€?/span>


濡傛灉瑙夊緱鏂囩珷鏈夌敤锛屾劅璋㈣€侀搧浠浆鍙戝垎浜紝璁╂洿澶氱殑灏忎紮浼村缓绔嬭繛鎺ワ紒


銆愬弬鑰冭祫鏂欍€?/span>

1 Java澶氱嚎绋嬬紪绋嬪疄鎴樻寚鍗楋紝 榛勬枃娴凤紝 涓浗宸ヤ俊鍑虹増闆嗗洟銆?/span>

2 Java骞跺彂缂栫▼浠庡叆闂ㄥ埌绮鹃€氾紝寮犳尟鍗庯紝娓呭崕澶у鍑虹増绀俱€?/span>



绋嬪簭鍛楥hatbook

          

绋嬪簭鍛橀兘鍏虫敞浜嗭紝鏉ヤ笉鍙婅В閲婏紝闀挎寜鍥剧墖锛屽揩涓婅溅


鏈枃寤朵几闃呰

涓婃枃1锛?/p>

涓婃枃2锛?/p>

鎺ㄨ崘1锛?/p>

鎺ㄨ崘2锛?/p>

以上是关于NO.36 璇诲啓鍒嗙:CopyOnWrite鏈哄埗的主要内容,如果未能解决你的问题,请参考以下文章

Rust 鏂囦欢璇诲啓

绠€鍗曞鏂囦欢鍐呭璇诲啓

IOI 2015 Teams 鍒嗙粍

濡備綍灏嗘暟鎹粠MySQL/MongoDB涓縼绉昏嚦浜戝紑鍙戞暟鎹簱

閫氳繃`RestTemplate`涓婁紶鏂囦欢(InputStreamResource璇﹁В)