Is there an analog of InitializeCriticalSectionWithSpinCount in Java? It implements a hybrid mutex. I have a problem seeing too many thread spending time in LockSupport.park() method while locking very short portions of computation intensive code with either ReentrantLock or synchronized (the code being locked takes hardly measurable portion of total running time on a single core). Here is a class that helps significantly my case, I wonder if I took wrong path.
public class ReentrantLockWithSpin extends ReentrantLock {
private final int _spinCount;
ReentrantLockWithSpin() {
_spinCount = 1000;
}
ReentrantLockWithSpin(int spinCount) {
_spinCount = spinCount;
}
@Override
public void lock() {
for (int spin = 0; spin != _spinCount; spin++) {
if (super.tryLock())
return;
Thread.yield();
}
super.lock();
}
}