据我了解,进行选择/ epoll / kqueue系统调用会阻塞进行该调用的线程或进程,直到套接字准备好进行IO或发生超时事件为止。在基于netty的程序上进行线程转储分析时,我看到父组线程在执行select调用后处于可运行状态,
"Parent Group1" #11 prio=5 os_prio=0 tid=0x000000001e575000 nid=0x3f98 runnable [0x000000001f2ce000]
java.lang.Thread.State: RUNNABLE
at sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method)
at sun.nio.ch.WindowsSelectorImpl$SubSelector.poll(WindowsSelectorImpl.java:296)
at sun.nio.ch.WindowsSelectorImpl$SubSelector.access$400(WindowsSelectorImpl.java:278)
at sun.nio.ch.WindowsSelectorImpl.doSelect(WindowsSelectorImpl.java:159)
at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
- locked <0x000000076bb59088> (a io.netty.channel.nio.SelectedSelectionKeySet)
- locked <0x000000076b9706b8> (a java.util.Collections$UnmodifiableSet)
- locked <0x000000076b970308> (a sun.nio.ch.WindowsSelectorImpl)
at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:101)
at io.netty.channel.nio.SelectedSelectionKeySetSelector.select(SelectedSelectionKeySetSelector.java:68)
at io.netty.channel.nio.NioEventLoop.select(NioEventLoop.java:803)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:457)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at java.lang.Thread.run(Thread.java:745)
即使Java线程正在等待系统调用还是我丢失了某些东西,Java线程也会显示为Runnable吗?
您可以从转储中看到,是的。
[Java线程只有在使用Java内置同步机制阻止后才可“运行”,例如等待进入监视器(例如Object.wait
,Thread.sleep
,Thread.join
)。
对select
/epoll
/ kqueue
和JNI的本地调用不会影响线程状态。