NIO[SelectableChannel.register和Selector.select会有锁等待冲突]

 

结论: register得放在select之前进行
Thread Dump信息:

"Poller-1" prio=6 tid=0x040f0400 nid=0x408 runnable [0x044df000]
   java.lang.Thread.State: RUNNABLE
        at sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method)
        at sun.nio.ch.WindowsSelectorImpl$SubSelector.poll(WindowsSelectorImpl.j
ava:295)
        at sun.nio.ch.WindowsSelectorImpl$SubSelector.access$400(WindowsSelector
Impl.java:277)
        at sun.nio.ch.WindowsSelectorImpl.doSelect(WindowsSelectorImpl.java:158)

        at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:87)
        - locked <0x23b7df70> (a sun.nio.ch.Util$2)
         - locked <0x23b7df60> (a java.util.Collections$UnmodifiableSet)
        - locked <0x23b7dd40> (a sun.nio.ch.WindowsSelectorImpl)
        at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:98)
        at com.zhh.nio.NioServer$Poller.run(NioServer.java:132)
        at java.lang.Thread.run(Thread.java:722)

"Poller-0" prio=6 tid=0x040f0000 nid=0x14e4 runnable [0x0439f000]
   java.lang.Thread.State: RUNNABLE
        at sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method)
        at sun.nio.ch.WindowsSelectorImpl$SubSelector.poll(WindowsSelectorImpl.j
ava:295)
        at sun.nio.ch.WindowsSelectorImpl$SubSelector.access$400(WindowsSelector
Impl.java:277)
        at sun.nio.ch.WindowsSelectorImpl.doSelect(WindowsSelectorImpl.java:158)

        at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:87)
        - locked <0x23b77a98> (a sun.nio.ch.Util$2)
        - locked <0x23b77a20> (a java.util.Collections$UnmodifiableSet)
        - locked <0x23b76aa0> (a sun.nio.ch.WindowsSelectorImpl)
        at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:98)
        at com.zhh.nio.NioServer$Poller.run(NioServer.java:132)
        at java.lang.Thread.run(Thread.java:722)

"Acceptor" prio=6 tid=0x040ef800 nid=0x1250 waiting for monitor entry [0x03f1f00
0]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at sun.nio.ch.SelectorImpl.register(SelectorImpl.java:133)
        -  waiting to lock <0x23b7df60> (a java.util.Collections$UnmodifiableSet)

        at java.nio.channels.spi.AbstractSelectableChannel.register(AbstractSele
ctableChannel.java:207)
        - locked <0x23b8da18> (a java.lang.Object)
        at java.nio.channels.SelectableChannel.register(SelectableChannel.java:2
77)
        at com.zhh.nio.NioSession.register(NioSession.java:50)
        at com.zhh.nio.NioServer$Poller.register(NioServer.java:123)
        at com.zhh.nio.NioServer.handleNewSession(NioServer.java:222)
        at com.zhh.nio.NioServer$Acceptor.run(NioServer.java:236)
        at java.lang.Thread.run(Thread.java:722)

Code层面:
select:
private int lockAndDoSelect(long timeout) throws IOException {
        synchronized (this) {
            if (!isOpen())
                throw new ClosedSelectorException();
             synchronized (publicKeys) {
                synchronized (publicSelectedKeys) {
                    return doSelect(timeout);
                }
            }
        }
    }
register:
 protected final SelectionKey register(AbstractSelectableChannel ch,
                                          int ops,
                                          Object attachment)
    {
        if (!(ch instanceof SelChImpl))
            throw new IllegalSelectorException();
        SelectionKeyImpl k = new SelectionKeyImpl((SelChImpl)ch, this);
        k.attach(attachment);
         synchronized (publicKeys) {
            implRegister(k);
        }
        k.interestOps(ops);
        return k;
    }

你可能感兴趣的:(selector)