`
加菲学Java
  • 浏览: 131189 次
  • 性别: Icon_minigender_1
  • 来自: 扬州
社区版块
存档分类
最新评论

SelectableChannel的register()方法和Selector的select()方法对资源的竞争问题

 
阅读更多
  无意中看到的, 原帖: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.
 

 
2.

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

 
   查看问题出在何处,首先执行jps查看运行的程序的pid.接下来使用jstack命令.如下:
 
C:\Users\yangkun>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 <0x00000000eb4b3cd8> (a java.util.Collections$Unmodifi
ableSet)
        at java.nio.channels.spi.AbstractSelectableChannel.register(Unknown Sour
ce)
        - locked <0x00000000eb4f2210> (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 <0x00000000eb405798> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(Unknown Source)
        - locked <0x00000000eb405798> (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 <0x00000000eb405320> (a java.lang.ref.Reference$Lock)
        at java.lang.Object.wait(Object.java:503)
        at java.lang.ref.Reference$ReferenceHandler.run(Unknown Source)
        - locked <0x00000000eb405320> (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 <0x00000000eb4b3d58> (a sun.nio.ch.Util$2)
        - locked <0x00000000eb4b3cd8> (a java.util.Collections$UnmodifiableSet)
        - locked <0x00000000eb4b3768> (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
 

 
 程序会一致阻塞而无法继续执行.
  • 大小: 19 KB
  • 大小: 10.1 KB
  • 大小: 18.9 KB
  • 大小: 69.9 KB
0
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics