DispatcherNpublic class DispatcherN extends Object implements DispatcherA Multi-threaded dispatcher.
In this example, one thread does accepts, and the second
does read/writes. |
Fields Summary |
---|
private Selector | sel | private Object | gate |
Constructors Summary |
---|
DispatcherN()
sel = Selector.open();
|
Methods Summary |
---|
private void | dispatch()
sel.select();
for (Iterator i = sel.selectedKeys().iterator(); i.hasNext(); ) {
SelectionKey sk = (SelectionKey)i.next();
i.remove();
Handler h = (Handler)sk.attachment();
h.handle(sk);
}
synchronized (gate) { }
| public void | register(java.nio.channels.SelectableChannel ch, int ops, Handler h)
synchronized (gate) {
sel.wakeup();
ch.register(sel, ops, h);
}
| public void | run()
for (;;) {
try {
dispatch();
} catch (IOException x) {
x.printStackTrace();
}
}
|
|