Does anyone know which of the two locking constructs is faster?
I have:
private static final Object mutex = new Object();
void method() {
synchronized(mutex) {
// code
}
}
vs:
BoundedSemaphore semaphore = new BoundedSemaphore(1);
void method() {
semaphore.take();
try{
//code
} finally {
semaphore.release();
}
}
Thanks, everyone.
Matt
In practice their performance is equivalent. The BoundedSemaphore in your example actually uses intrinsic locking similar to your first example. Thus they wouldn’t vary noticeably.