Package org.jppf.nio
Class SelectorSynchronizerLock
java.lang.Object
org.jppf.nio.SelectorSynchronizerLock
- All Implemented Interfaces:
SelectorSynchronizer
Synchronization aid for a
Selector, to prevent SelectionKey.interestOps(int) invocations from blocking for too long.
Usage pattern:
- in the selection loop:
SelectorSynchronizer sync = new SelectorSynchronizerLock(selector);
try {
int n = 0;
while (true) {
sync.waitForZeroAndSetToMinusOne();
try {
n = selector.select(selectTimeout);
} finally {
sync.setToZeroIfNegative();
}
if (n > 0) {
// process selected keys
}
}
} catch (Exception e) {
// process exception
}
- in separate threads:
public void setInterestOps(SelectionKey key, int newOps) throws Exception {
sync.wakeUpAndSetOrIncrement();
try {
key.interestOps(newOps);
} finally {
sync.decrement();
}
}
- Author:
- Laurent Cohen
-
Constructor Summary
ConstructorsConstructorDescriptionSelectorSynchronizerLock(Selector selector) Initialize with the specified selector. -
Method Summary
Modifier and TypeMethodDescriptionvoidDecrement the value.voidSet the value to zero if it is less than 0.voidWait for the value to go down to zero, then set it to -1.voidCompare the value with the expected value, and run an action and increment the value if the comparison succeeds, or set the value to the update value if it does not succeed.
-
Constructor Details
-
SelectorSynchronizerLock
Initialize with the specified selector.- Parameters:
selector- the selector to wakeup.
-
-
Method Details
-
waitForZeroAndSetToMinusOne
public void waitForZeroAndSetToMinusOne()Description copied from interface:SelectorSynchronizerWait for the value to go down to zero, then set it to -1.- Specified by:
waitForZeroAndSetToMinusOnein interfaceSelectorSynchronizer
-
setToZeroIfNegative
public void setToZeroIfNegative()Description copied from interface:SelectorSynchronizerSet the value to zero if it is less than 0.- Specified by:
setToZeroIfNegativein interfaceSelectorSynchronizer
-
decrement
public void decrement()Description copied from interface:SelectorSynchronizerDecrement the value.- Specified by:
decrementin interfaceSelectorSynchronizer
-
wakeUpAndSetOrIncrement
public void wakeUpAndSetOrIncrement()Description copied from interface:SelectorSynchronizerCompare the value with the expected value, and run an action and increment the value if the comparison succeeds, or set the value to the update value if it does not succeed.- Specified by:
wakeUpAndSetOrIncrementin interfaceSelectorSynchronizer
-