阅读525 返回首页    go 阿里云 go 技术社区[云栖]


Bug:StampedLock鐨勪腑鏂梾棰桦镊碈PU鐖嗘弧-鍗氩-浜戞尔绀惧尯-阒块噷浜?

StampedLock浣滀负JAVA8涓嚭鐜扮殑鏂板瀷阌侊纴寰埚彲鑳藉湪澶у鏁板満鏅兘鍙互镟夸唬ReentrantReadWriteLock銆傚畠瀵逛簬璇?鍐欓兘鎻愪緵浜嗗洓涓帴鍙?鎹㈡垚write涓哄啓阌?锛?/p>

  • readLock()
  • tryReadLock()
  • tryReadLock(long time, TimeUnit unit)
  • readLockInterruptibly()

杩椤嚑涓柟娉曞搴旂殑璇箟涓猴细

  • 銮峰彇璇婚挛锛堥樆濉烇纴涓嶅搷搴斾腑鏂级
  • 銮峰彇璇婚挛锛堢珛鍗筹级
  • 闄愭椂銮峰彇璇婚挛锛埚搷搴斾腑鏂级
  • 銮峰彇璇婚挛锛堥樆濉烇纴鍝嶅簲涓柇锛?/li>

铹惰€屽湪readLock鏂规硶锛埚嵆涓嶅搷搴斾腑鏂级涓瓨鍦ㄩ梾棰?write鐨勭増链篃鏄?锛岃瀵烠PU浣跨敤鐜囷纴鎵ц浠ヤ笅浠g爜锛?/div>
01 publicclassTestStampedLock {
02 听听听听publicstaticvoidmain(String[] args)听throwsInterruptedException{
03 听听听听finalStampedLock lock =听newStampedLock();
04 听听听听newThread(){
05 听听听听听听听publicvoidrun(){
06 听听听听听听听longreadLong = lock.writeLock();
07 听听听听听听听LockSupport.parkNanos(6100000000L);
08 听听听听听听听lock.unlockWrite(readLong);
09 听听听听听}
10 听听听听}.start();
11 听听听听Thread.sleep(100);
12 听听听听for(听inti =听0; i <听3; ++i)
13 听听听听听听听newThread(newOccupiedCPUReadThread(lock)).start();
14 听听听听}
15 听听听听privatestaticclassOccupiedCPUReadThread听implementsRunnable{
16 听听听听听听听听privateStampedLock lock;
17 听听听听听听听听publicOccupiedCPUReadThread(StampedLock lock){
18 听听听听听听听听听听听听this.lock = lock;
19 听听听听听听听听}
20 听听听听听听听听publicvoidrun(){
21 听听听听听听听听听听听听Thread.currentThread().interrupt();
22 听听听听听听听听听听听听longlockr = lock.readLock();
23 听听听听听听听听听听听听System.out.println(Thread.currentThread().getName() +听" get read lock");
24 听听听听听听听听听听听听lock.unlockRead(lockr);
25 听听听听听听听听}
26 听听听听}
27 }
鍏埚紑钖竴涓嚎绋嬭幏鍙栧啓阌佸苟淇濇寔6绉掞纴鍐嶅紑钖笁涓甫镌€涓柇钟舵€佺殑绾跨▼铡昏幏鍙栬阌侊纸readLock鏂规硶锛夛纴缁撴灉鏄?涓牳蹇冭鍗犳嵁浜呜繎6绉掋€?/div>
铡熷洜鍦ㄤ簬娌℃湁浣跨敤淇濆瓨/澶嶅师涓柇钟舵€?/strong>鐨勬満鍒讹纴阃氲绷hack婧愮爜锛屾彃鍏ヤ缭瀛树腑鏂拰杩斿洖鍓嶆仮澶崭腑鏂殑鐩稿叧浠g爜鍗冲彲淇锛?/div>
1 booleaninterrupted听=听false;
1 if(interrupted)
2 听听听听Thread.currentThread().interrupt();
3 returnns;
1 if(Thread.interrupted()){
2 听听听听if(interruptible)
3 听听听听听听听听returncancelWaiter(node, p,听true);
4 听听听听else
5 听听听听听听听听interrupted =听true;
6 }
  • 杞浇镊?a href="http://ifeve.com/" target="_blank" >骞跺彂缂栫▼缃?- ifeve.com

鐢ㄤ簯镙栫ぞ鍖篈PP锛岃垝链崀

銆愪簯镙栧揩璁€戝浣曞簲鑱树簰鑱旂綉鍏徃锛屾嬁鍒伴珮钖狾ffer锛熻亴鍦鸿彍楦熷浣曞揩阃熸垚闀匡纻阒块噷瀛﹂暱涓轰綘鏀嫑锛屽湪绾跨洿鎾纴瓒充笉鍑烘埛鐪嬬璇€锛孖T鏂板叺蹇潵钖э紒  璇︽儏璇风偣鍑?/a>
璇勮鏂囩珷 (0) (0) (0)
鍒嗕韩鍒?

缃戝弸璇勮

  • 鐩綍
鏀惰捣
  • 鐗堟潈澹版槑
链ぞ鍖哄唴瀹圭敱浜掕仈缃戠敤鎴疯嚜鍙戣础鐚纴链ぞ鍖轰笉鎷ユ湁鎵€链夋潈锛屼篃涓嶆圹鎷呯浉鍏虫硶寰嬭矗浠汇€傚鏋沧偍鍙戠幇链ぞ鍖轰腑链夋秹瀚屾妱琚殑鍐呭锛屾杩庡彂阃侀偖浠讹细zhaoyunqi@list.alibaba-inc.com 杩涜涓炬姤锛屽苟鎻愪緵鐩稿叧璇佹嵁锛屼竴缁忔煡瀹烇纴链ぞ鍖哄皢绔嫔埢鍒犻櫎娑夊珜渚垫潈鍐呭銆?

最后更新:2017-05-23 18:02:56

  上一篇:go  Java 8:StampedLock、ReadWriteLock以及synchronized的比较
  下一篇:go  JDK的sql设计不合理导致的驱动类初始化死锁问题