// When finished, invoker must ensure that selector is empty // by cancelling any related keys and explicitly releasing // the selector by invoking releaseTemporarySelector() static Selector getTemporarySelector(SelectableChannel sc) throws IOException { SoftReference ref = (SoftReference) localSelector.get(); SelectorWrapper selWrapper = null; Selector sel = null; if (ref == null || ((selWrapper = (SelectorWrapper) ref.get()) == null) || ((sel = selWrapper.get()) == null) || (sel.provider() != sc.provider())) { sel = sc.provider().openSelector(); localSelector.set(new SoftReference(new SelectorWrapper(sel))); } else { localSelectorWrapper.set(selWrapper); } return sel; }
public void run() { try { sel.close(); } catch (Throwable th) { throw new Error(th); } }
static void releaseTemporarySelector(Selector sel) throws IOException { // Selector should be empty sel.selectNow(); // Flush cancelled keys assert sel.keys().isEmpty() : "Temporary selector not empty"; localSelectorWrapper.set(null); }