Unstructured Lock
Java提供另一種比較彈性的lock機制: Lock interface。Lock object跟每個object的intrinsic lock沒什不同,也是有wait/notify,但有個tryLock的method,會return一個boolean,這讓嘗試要acquire lock的thread不會被block住,可以去做其他的事,之後再來try。
但是麻煩的就是獲得lock的人一定要想辦法在對的地方呼叫unlock,即便是exception發生 (永遠在finally clause做unlock),由於不會block structure自動acquire/release lock,所以稱為unstructured lock。
Lock l = ...;
l.lock();
try {
// access the resource protected by this lock
} finally {
l.unlock();
}
ReentrantLock
這是一個implement Lock interface的class。
ReadWriteLock
這比較特別,他能提供concurrent read lock,也就是多個thread 能夠同時acquire這個read lock的時候,可以concurrent執行被這個read lock "保護"的code,但是只有一個thread能acquire write lock,而且一旦write lock被acquire,則acuire read lock的threads全部被block,直到write lock被unlock。
Performance Comparison: Structured vs Unstructured
我們來比較在四核心上,structured vs unstructured對linked list的 read/write performance比較:
CoarseList是使用ReentrantLock ,RWCoarseList是使用ReentrantReadWriteLock ,而SyncList是使用intrinsic lock,以SyncList當baseline
(1) CoarseList vs. SyncList (Large Random)
=========================================================
1.0839688041594453x improvement in add throughput
0.9205057152753724x improvement in contains throughput
0.8414179104477613x improvement in remove throughput
=========================================================
可以看到overall ReentrantLock的overhead比intrinsic lock大一些。
(2) RWCoarseList vs. SyncList (Large Random)
=========================================================
0.7537446750034354x improvement in add throughput
3.2617096018735365x improvement in contains throughput
0.9670546105640107x improvement in remove throughput
=========================================================
ReentrantReadWriteLock 可以讓read operation的次數多很多,達到3倍以上。
(3) RWCoarseList vs. SyncList (Small Random)
=========================================================
1.1236133122028527x improvement in add throughput
3.2494758909853254x improvement in contains throughput
0.9303955933900853x improvement in remove throughput
=========================================================
(4) CoarseList vs. SyncList (Large Repeating)
=========================================================
0.7279840555104452x improvement in add throughput
0.7392840160315196x improvement in contains throughput
1.5410764872521248x improvement in remove throughput
=========================================================
(5) RWCoarseList vs. SyncList (Large Repeating)
=========================================================
0.7709759000297531x improvement in add throughput
3.6224951519069166x improvement in contains throughput
0.8615888615888616x improvement in remove throughput
=========================================================
(6) RWCoarseList vs. SyncList (Small Repeating)
=========================================================
0.6785995899700362x improvement in add throughput
3.8316566063044935x improvement in contains throughput
1.2968299711815563x improvement in remove throughput
=========================================================
沒有留言:
張貼留言