1 7 8 package java.nio.channels.spi; 9 10 import java.io.IOException ; 11 import java.nio.channels.SelectionKey ; 12 import java.nio.channels.Selector ; 13 import java.util.HashSet ; 14 import java.util.Set ; 15 import sun.nio.ch.Interruptible; 16 import java.util.concurrent.atomic.AtomicBoolean ; 17 18 19 51 52 public abstract class AbstractSelector 53 extends Selector 54 { 55 56 private AtomicBoolean selectorOpen = new AtomicBoolean (true); 57 58 private final SelectorProvider provider; 60 61 64 protected AbstractSelector(SelectorProvider provider) { 65 this.provider = provider; 66 } 67 68 private final Set cancelledKeys = new HashSet (); 69 70 void cancel(SelectionKey k) { synchronized (cancelledKeys) { 72 cancelledKeys.add(k); 73 } 74 } 75 76 87 public final void close() throws IOException { 88 boolean open = selectorOpen.getAndSet(false); 89 if (!open) 90 return; 91 implCloseSelector(); 92 } 93 94 110 protected abstract void implCloseSelector() throws IOException ; 111 112 public final boolean isOpen() { 113 return selectorOpen.get(); 114 } 115 116 121 public final SelectorProvider provider() { 122 return provider; 123 } 124 125 132 protected final Set <SelectionKey > cancelledKeys() { 133 return cancelledKeys; 134 } 135 136 155 protected abstract SelectionKey register(AbstractSelectableChannel ch, 156 int ops, Object att); 157 158 167 protected final void deregister(AbstractSelectionKey key) { 168 ((AbstractSelectableChannel )key.channel()).removeKey(key); 169 } 170 171 172 174 private Interruptible interruptor = null; 175 176 189 protected final void begin() { 190 if (interruptor == null) { 191 interruptor = new Interruptible() { 192 public void interrupt() { 193 AbstractSelector.this.wakeup(); 194 }}; 195 } 196 AbstractInterruptibleChannel.blockedOn(interruptor); 197 if (Thread.currentThread().isInterrupted()) 198 interruptor.interrupt(); 199 } 200 201 209 protected final void end() { 210 AbstractInterruptibleChannel.blockedOn(null); 211 } 212 213 } 214 | Popular Tags |