Package org.jppf.nio
Class SelectorSynchronizerImpl
java.lang.Object
org.jppf.nio.SelectorSynchronizerImpl
- All Implemented Interfaces:
SelectorSynchronizer
Synchronization aid for a
Selector, to prevent SelectionKey.interestOps(int) invocations from blocking for too long.
This implementation synchronizes on itself.
Usage pattern:
- in the selection loop:
SelectorSynchronizer sync = new SelectorSynchronizerImpl(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
ConstructorsConstructorDescriptionSelectorSynchronizerImpl(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 run an action and incrment the value if the comparison succeeds, or set the value to the update value if it does not succeed..
-
Constructor Details
-
SelectorSynchronizerImpl
Initialize with the specified selector.- Parameters:
selector- the selector to wakeup.
-
-
Method Details
-
waitForZeroAndSetToMinusOne
public void waitForZeroAndSetToMinusOne()Wait for the value to go down to zero, then set it to -1.- Specified by:
waitForZeroAndSetToMinusOnein interfaceSelectorSynchronizer
-
setToZeroIfNegative
public void setToZeroIfNegative()Set the value to zero if it is less than 0.- Specified by:
setToZeroIfNegativein interfaceSelectorSynchronizer
-
decrement
public void decrement()Decrement the value.- Specified by:
decrementin interfaceSelectorSynchronizer
-
wakeUpAndSetOrIncrement
public void wakeUpAndSetOrIncrement()Compare the value with the expected value, and run run an action and incrment the value if the comparison succeeds, or set the value to the update value if it does not succeed..- Specified by:
wakeUpAndSetOrIncrementin interfaceSelectorSynchronizer
-