Get a exclusive Selector
try{
for (int i = 0; i < maxSelectors; i++)
selectors.add(Selector.open());
} catch (IOException ex){
}
synchronized(selectors) {
Selector s = null;
try {
if ( selectors.size() != 0 )
s = selectors.pop();
} catch (EmptyStackException ex){}
int attempts = 0;
try{
while (s == null && attempts < 2) {
selectors.wait(timeout);
try {
if ( selectors.size() != 0 )
s = selectors.pop();
} catch (EmptyStackException ex){
break;
}
attempts++;
}
} catch (InterruptedException ex){};
return s;
}