[英]BoundedSemaphore semaphore vs standard Mutex
有誰知道這兩種鎖定結構中的哪一種更快? 我有:
private static final Object mutex = new Object();
void method() {
synchronized(mutex) {
// code
}
}
與:
BoundedSemaphore semaphore = new BoundedSemaphore(1);
void method() {
semaphore.take();
try{
//code
} finally {
semaphore.release();
}
}
感謝大家。 馬特
在實踐中,它們的性能是相當的。 您示例中的 BoundedSemaphore 實際上使用類似於您的第一個示例的內部鎖定。 因此它們不會有明顯的變化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.