閱讀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設計不合理導致的驅動類初始化死鎖問題