Java NIO UDP - Selector Hangs when trying to register
final DatagramChannel dc = DatagramChannel.open();
dc.configureBlocking(false);
dc.socket().bind(localAddress);
final SelDatagramChannel c = new SelDatagramChannel(dc, datagramListener);
--->>>> final SelectionKey sk = dc.register(selector, SelectionKey.OP_READ);
The marke开发者_运维技巧t line hangs forever when you try to register a DatagramChannel.
Does anyone know why?
There's some chat in http://echelog.matzon.dk/logs/browse/java/1276898400 which mentions that dc.register()
must be called in the selection thread. The JavaDoc doesn't seem very specific about that, though.
I guess you do that by putting in a list somewhere, and calling selector.wakeup()
. But I'm still having timing glitches where it's unclear how long before the register succeeds...
Have you tried sending a CTRL+BREAK to the JVM to get a thread dump ?
Is the above code executed in the thread which does selection? I think we need to register the channels with Selector in the same thread which does selection.
精彩评论