Package org.jppf.nio
Interface SelectorSynchronizer
- All Known Implementing Classes:
SelectorSynchronizerImpl,SelectorSynchronizerLock
public interface 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 = ...;
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
-
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.
-
Method Details
-
waitForZeroAndSetToMinusOne
void waitForZeroAndSetToMinusOne()Wait for the value to go down to zero, then set it to -1. -
setToZeroIfNegative
void setToZeroIfNegative()Set the value to zero if it is less than 0. -
decrement
void decrement()Decrement the value. -
wakeUpAndSetOrIncrement
void wakeUpAndSetOrIncrement()Compare 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.
-