Java线程的状态有五种。 其中,IO流操作的一些接口是阻止操作的示例
package com.cqs.example.io.stream;
import lombok.extern.slf4j.slf4j;
import java.util.Scanner;
import Java.util.concurrent.atomic.atomic long;
//*
*
* @Author lixw
* @Date 7/22/20 11:49 PM
*/
@Slf4j
公共类iostreamstate {
staticclassscannertaskimplementsrunnable {
公共void run (}
sanner scanner=new scanner (system.in;
//一直在这里等待输入
wile(Scanner.Hasnext () ) ) )。
log.info (处理信息:{} )、scanner.next );
}
}
}
staticclasshighcpuimplementsrunnable {
公共void run (}
AtomicLong cnt=new AtomicLong (;
//一直在这里等待输入
while (真) {
if(CNT.addandget(1) % 10000000==0) {
log.info(Helloword );
Thread.yield (;
}
if(CNT.get(==long.max_value ) {
CNT.set(0;
}
}
}
}
publicstaticvoidmain (字符串[ ] args ) throws InterruptedException { )。
threadt=new thread (新闻扫描任务)、' block-io ' );
t.start (;
threadT2=newthread(newhighCPU )、' high-cpu );
T2 .开始(;
t.join (;
}
}
通过查看名为scanner.next ()的行中的代码,可以继续到此(FileInputStream.java ) :
//*
* readsuptolenbytesofdatafromthisinputstream
* intoanarrayofbytes.iflenisnotzero,the method
* blocksuntilsomeinputisavailable; 以太网,否
* bytes are read and 0 is returned。
*
* @ parambthebufferintowhichthedataisread。
* @ paramoffthestartoffsetinthedestinationarrayb
* @ paramlenthemaximumnumberofbytesread。
* @ returnthetotalnumberofbytesreadintothebuffer,or
*-1 ifthereisnomoredatabecausetheendof
* the file has been reached。
* @ exceptionnullpointerexceptionifbisnull。
* @ exceptionindexoutofboundsexceptionifoffisnegative,
* len is negative,or len is greater than
* b .长关
* @ exceptionioexceptionifani/oerroroccurs。
*/
公共int read (byteb [ ],int off,int len ) throws IOException {
返回读取字节(b,off,len );
}
这个方法写得很清楚的是,它会一直阻止到有输入数据为止。
验证线程的状态
li@cqs:~$ jps -l
7110 com.cqs.example.io.stream.iostreamstate
使用jstack查找线程
image.png
JVM下行线程的状态竟然是Runnable
不惊讶,意外地?
将“阻止- io”线程的(NID=0x6a 07==本地线程id )转换为十进制
Li @ cqs :~$ printf ' % dn '0x6a 07
27143
结合top命令查看
li@cqs:~$ top -Hp 27110
Threads: 21 total,1 running,20 sleeping,0 stopped,
%CPU(s ) : 16.8 us、0.2 sy、0.0 ni、79.3 id、0.0 wa、0.0 hi、
KiB Mem : 24282792 total、12235680 free、5818508 used、62286
KiB Swap: 2097148 total,2097148 free,0 used. 170711
PID USER PR NI VIRT RES SHR S %CPU %MEM
27144 Li 20090478766732020460 r 99.90.3
27110 Li 20090478766732020460 s0.0. 3
7111 Li 20090478766732020460 s0.0. 3
7118 Li 20090478766732020460 s0.0. 3
7119 Li 20090478766732020460 s0.0. 3
27120 Li 20090478766732020460 s0.0. 3
7121 Li 20090478766732020460 s0.0. 3
7122 Li 20090478766732020460 s0.0. 3
7123 Li 20090478766732020460 s0.0. 3
27125 Li 20090478766732020460 s0.0. 3
7127 Li 20090478766732020460 s0.0. 3
7129 Li 20090478766732020460 s0.0. 3
7136 Li 20090478766732020460 s0.0. 3
7137 Li 20090478766732020460 s0.0. 3
7138 Li 20090478766732020460 s0.0. 3
2139 Li 20090478766732020460 s0.0. 3
7140 Li 20090478766732020460 s0.0. 3
7141 Li 20090478766732020460 s0.0. 3
7142 Li 20090478766732020460 s0.0. 3
7143 Li 20090478766732020460 s0.0. 3
7155 Li 20090478766732020460 s0.0. 3
27143可见的状态为' s '
同样,可以验证与“高CPU”相对应的本机线程为27144,该线程的执行状态正好为r,CPU的消耗也较高
image.png
结论:
Java流中的方法指向内核状态线程的状态,而不是JVM线程的状态