首先,问问题的人和回答问题的人应该都没有好好的仔细看书.书中已经明确地说到:
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
|
程序会一致阻塞而无法继续执行.
相关推荐
Selector
在simulink上进行BusCreator和BusSelector的简单实验
探索Android中selector和shape的结合使用
Android selector 完整demo
在插件使用方面,file_selector插件可以方便地实现文件选择功能,支持多选和所有类型的文件,并且在选择文件后可以获取到文件的路径和其他信息。 在使用file_selector插件时,需要注意以下几点: 在Android系统中...
代码实现drawable的selector效果,不用为每个控件写selector样式; 另外,实现圆形图片处理、圆角图片处理功能
一个强大的selector注入器,它可以让view自动产生selector状态,免去了你写selector的麻烦。
主要介绍了Android使用selector修改TextView中字体颜色和背景色的方法,实例分析了selector方法的相关使用技巧,需要的朋友可以参考下
IPhone对@selector的函数如何传参数
NULL 博文链接:https://goon.iteye.com/blog/1775421
selenium css selector 定位详解
用selector设置button可用和不可用的样式
CSS Selector 生成器
http://blog.csdn.net/zhufuing/article/details/20134255博客的demo下载
带圆角的selector
支持SVG和TINT,并且二者可以配合使用 可以开启水波纹(ripple)按压效果 > 如果你的需求很简单,不妨试试「这篇文章」提到的一张图片实现selector的方案。 引入 1.添加JitPack仓库 repositories { maven { url ...
selectorgadget插件,R语言爬取网页
java selector 测试并发.多线程并发。
Laravel开发-selector 选择器模式抽象类,用于管理用户级或运行时参数上的多态性。
一个关于SocketChannel、ServerSocketChannel、Selector的综合案例