1 36 37 import java.io.*; 38 import java.nio.channels.*; 39 import java.util.*; 40 41 51 class DispatcherN implements Dispatcher { 52 53 private Selector sel; 54 55 DispatcherN() throws IOException { 56 sel = Selector.open(); 57 } 58 59 public void run() { 60 for (;;) { 61 try { 62 dispatch(); 63 } catch (IOException x) { 64 x.printStackTrace(); 65 } 66 } 67 } 68 69 private Object gate = new Object (); 70 71 private void dispatch() throws IOException { 72 sel.select(); 73 for (Iterator i = sel.selectedKeys().iterator(); i.hasNext(); ) { 74 SelectionKey sk = (SelectionKey)i.next(); 75 i.remove(); 76 Handler h = (Handler)sk.attachment(); 77 h.handle(sk); 78 } 79 synchronized (gate) { } 80 } 81 82 public void register(SelectableChannel ch, int ops, Handler h) 83 throws IOException { 84 synchronized (gate) { 85 sel.wakeup(); 86 ch.register(sel, ops, h); 87 } 88 } 89 90 } 91 | Popular Tags |