---
title: "SelectableChannel的register()方法和Selector的select()方法对资源的竞争问题"
author: "加菲学Java"
date: "2013-10-27T21:50:13Z"
tags: []
categories: ["编程语言"]
slug: "selectablechannel-register-selector-select"
draft: false
lastmod: "2013-10-27T21:50:13Z"
id: 1965443
url: "https://yklovejava-163-com.iteye.com/blog/1965443"
---

无意中看到的, 原帖：[http://www.oschina.net/code/snippet\_246601\_22883](http://www.oschina.net/code/snippet_246601_22883)

  首先,问问题的人和回答问题的人应该都没有好好的仔细看书.书中已经明确地说到：

SelectableChannel的register(Selector selector, ...)和Selector的select()方法都会操作Selector对象的共享资源all-keys集合.

SelectableChannel及Selector的实现对操作共享资源的代码块进行了同步,从而避免了对共享资源的竞争.

同步机制使得一个线程执行SelectableChannel的register(Selector selctor, ...)时,

不允许另一个线程同时执行Selector的select()方法,反之亦然.

  并且说明解释的也很清楚.

  既然这么说了,那看完书中的代码之后就应该对其进行一些改造,这样才能更好的理解程序.

  为了解答这里的问题,对例子稍微一些改动：

1\.

   
![](http://dl2.iteye.com/upload/attachment/0090/6062/88f01bb1-e4e0-32cc-ad31-cccce1e8c566.png)  
 

2\.

![](http://dl2.iteye.com/upload/attachment/0090/6066/198660b1-cdda-35c4-b586-cc62cf6334b3.png)  
 

  然后运行程序,这时候​可以看到出现问题了：

![](http://dl2.iteye.com/upload/attachment/0090/6068/900807ab-360e-3696-8e63-2b9f775e1391.png)  
 

   查看问题出在何处,首先执行jps查看运行的程序的pid.接下来使用jstack命令.如下：

 

C:\\Users\\yangkun&gt;jstack 10008

2013-10-27 15:37:16

Full thread dump Java HotSpot(TM) 64-Bit Server VM (23.5-b02 mixed mode):

 

"Thread-0" prio=6 tid=0x000000000ac26800 nid=0x1dd8 waiting for monitor entry [0

x000000000b25f000]

   java.lang.Thread.State: BLOCKED (on object monitor)

        at sun.nio.ch.SelectorImpl.register(Unknown Source)

        - waiting to lock &lt;0x00000000eb4b3cd8&gt; (a java.util.Collections$Unmodifi

ableSet)

        at java.nio.channels.spi.AbstractSelectableChannel.register(Unknown Sour

ce)

        - locked &lt;0x00000000eb4f2210&gt; (a java.lang.Object)

        at java.nio.channels.SelectableChannel.register(Unknown Source)

        at tcp.selector.SelectorDeadLock.accept(SelectorDeadLock.java:47)

        at tcp.selector.SelectorDeadLock$1.run(SelectorDeadLock.java:83)

 

"Service Thread" daemon prio=6 tid=0x0000000008fa8000 nid=0x2a4c runnable [0x000

0000000000000]

   java.lang.Thread.State: RUNNABLE

 

"C2 CompilerThread1" daemon prio=10 tid=0x0000000008f9c000 nid=0x29b0 waiting on

 condition \[0x0000000000000000]

   java.lang.Thread.State: RUNNABLE

 

"C2 CompilerThread0" daemon prio=10 tid=0x0000000008f9a000 nid=0x29ac waiting on

 condition \[0x0000000000000000]

   java.lang.Thread.State: RUNNABLE

 

"Attach Listener" daemon prio=10 tid=0x0000000008f99000 nid=0x29b4 waiting on co

ndition \[0x0000000000000000]

   java.lang.Thread.State: RUNNABLE

 

"Signal Dispatcher" daemon prio=10 tid=0x0000000008f92000 nid=0x2a48 runnable [0

x0000000000000000]

   java.lang.Thread.State: RUNNABLE

 

"Finalizer" daemon prio=8 tid=0x00000000025a6000 nid=0x2a40 in Object.wait() [0x

000000000a37f000]

   java.lang.Thread.State: WAITING (on object monitor)

        at java.lang.Object.wait(Native Method)

        - waiting on &lt;0x00000000eb405798&gt; (a java.lang.ref.ReferenceQueue$Lock)

        at java.lang.ref.ReferenceQueue.remove(Unknown Source)

        - locked &lt;0x00000000eb405798&gt; (a java.lang.ref.ReferenceQueue$Lock)

        at java.lang.ref.ReferenceQueue.remove(Unknown Source)

        at java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)

 

"Reference Handler" daemon prio=10 tid=0x000000000259f000 nid=0x2a3c in Object.w

ait() \[0x000000000a1ef000]

   java.lang.Thread.State: WAITING (on object monitor)

        at java.lang.Object.wait(Native Method)

        - waiting on &lt;0x00000000eb405320&gt; (a java.lang.ref.Reference$Lock)

        at java.lang.Object.wait(Object.java:503)

        at java.lang.ref.Reference$ReferenceHandler.run(Unknown Source)

        - locked &lt;0x00000000eb405320&gt; (a java.lang.ref.Reference$Lock)

 

"main" prio=6 tid=0x000000000264c800 nid=0x25a8 runnable \[0x000000000296e000]

   java.lang.Thread.State: RUNNABLE

        at sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method)

        at sun.nio.ch.WindowsSelectorImpl$SubSelector.poll(Unknown Source)

        at sun.nio.ch.WindowsSelectorImpl$SubSelector.access$400(Unknown Source)

 

        at sun.nio.ch.WindowsSelectorImpl.doSelect(Unknown Source)

        at sun.nio.ch.SelectorImpl.lockAndDoSelect(Unknown Source)

        - locked &lt;0x00000000eb4b3d58&gt; (a sun.nio.ch.Util$2)

        - locked &lt;0x00000000eb4b3cd8&gt; (a java.util.Collections$UnmodifiableSet)

        - locked &lt;0x00000000eb4b3768&gt; (a sun.nio.ch.WindowsSelectorImpl)

        at sun.nio.ch.SelectorImpl.select(Unknown Source)

        at sun.nio.ch.SelectorImpl.select(Unknown Source)

        at tcp.selector.SelectorDeadLock.service(SelectorDeadLock.java:62)

        at tcp.selector.SelectorDeadLock.main(SelectorDeadLock.java:89)

 

"VM Thread" prio=10 tid=0x0000000008ef2800 nid=0x2a38 runnable

 

"GC task thread#0 (ParallelGC)" prio=6 tid=0x00000000024f3000 nid=0x2144 runnabl

e

 

"GC task thread#1 (ParallelGC)" prio=6 tid=0x00000000024f5000 nid=0x2344 runnabl

e

 

"GC task thread#2 (ParallelGC)" prio=6 tid=0x00000000024f6800 nid=0x2318 runnabl

e

 

"GC task thread#3 (ParallelGC)" prio=6 tid=0x00000000024f8800 nid=0x22c4 runnabl

e

 

"VM Periodic Task Thread" prio=10 tid=0x0000000008fb1800 nid=0x249c waiting on c

ondition

 

JNI global references: 125

 

![](http://dl2.iteye.com/upload/attachment/0090/6071/c48c9d0d-0fdc-3875-8d6f-7d56988e3941.png)  
 

 程序会一致阻塞而无法继续执行.