Java线程的状态有五种.其中IO流操作一些接口是阻塞操作的例如
package com.cqs.example.io.stream;
import lombok.extern.slf4j.Slf4j;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicLong;
/**
*
* @Author lixw
* @Date 7/22/20 11:49 PM
*/
@Slf4j
public class IOStreamState {
static class ScannerTask implements Runnable {
public void run() {
Scanner scanner = new Scanner(System.in);
//一直停留在这里 等待着输入
while (scanner.hasNext()) {
log.info(" 处理信息:{}", scanner.next());
}
}
}
static class HighCpu implements Runnable {
public void run() {
AtomicLong cnt = new AtomicLong();
//一直停留在这里 等待着输入
while (true) {
if (cnt.addAndGet(1) % 10000000 == 0) {
log.info("hello word");
Thread.yield();
}
if (cnt.get() == Long.MAX_VALUE) {
cnt.set(0);
}
}
}
}
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(new ScannerTask(),"block-io");
t.start();
Thread t2 = new Thread(new HighCpu(),"high-cpu");
t2.start();
t.join();
}
}
看scanner.next()这行代码可以一直跟到这( FileInputStream.java):
/**
* Reads up to <code>len</code> bytes of data from this input stream
* into an array of bytes. If <code>len</code> is not zero, the method
* blocks until some input is available; otherwise, no
* bytes are read and <code>0</code> is returned.
*
* @param b the buffer into which the data is read.
* @param off the start offset in the destination array <code>b</code>
* @param len the maximum number of bytes read.
* @return the total number of bytes read into the buffer, or
* <code>-1</code> if there is no more data because the end of
* the file has been reached.
* @exception NullPointerException If <code>b</code> is <code>null</code>.
* @exception IndexOutOfBoundsException If <code>off</code> is negative,
* <code>len</code> is negative, or <code>len</code> is greater than
* <code>b.length - off</code>
* @exception IOException if an I/O error occurs.
*/
public int read(byte b[], int off, int len) throws IOException {
return readBytes(b, off, len);
}
这个方法清清楚楚写的是一直block直到有输入数据.
那么验证线程的状态
li@cqs:~$ jps -l
27110 com.cqs.example.io.stream.IOStreamState
使用jstack找到线程
image.png
JVM的下线程状态居然是Runnable
惊不惊喜 意外不意外?
将"block-io"线程的(nid=0x6a07 ==> native 线程ID) 转成10进制
li@cqs:~$ printf '%d\n' 0x6a07
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 20 0 9047876 67320 20460 R 99.9 0.3
27110 li 20 0 9047876 67320 20460 S 0.0 0.3
27111 li 20 0 9047876 67320 20460 S 0.0 0.3
27118 li 20 0 9047876 67320 20460 S 0.0 0.3
27119 li 20 0 9047876 67320 20460 S 0.0 0.3
27120 li 20 0 9047876 67320 20460 S 0.0 0.3
27121 li 20 0 9047876 67320 20460 S 0.0 0.3
27122 li 20 0 9047876 67320 20460 S 0.0 0.3
27123 li 20 0 9047876 67320 20460 S 0.0 0.3
27125 li 20 0 9047876 67320 20460 S 0.0 0.3
27127 li 20 0 9047876 67320 20460 S 0.0 0.3
27129 li 20 0 9047876 67320 20460 S 0.0 0.3
27136 li 20 0 9047876 67320 20460 S 0.0 0.3
27137 li 20 0 9047876 67320 20460 S 0.0 0.3
27138 li 20 0 9047876 67320 20460 S 0.0 0.3
27139 li 20 0 9047876 67320 20460 S 0.0 0.3
27140 li 20 0 9047876 67320 20460 S 0.0 0.3
27141 li 20 0 9047876 67320 20460 S 0.0 0.3
27142 li 20 0 9047876 67320 20460 S 0.0 0.3
27143 li 20 0 9047876 67320 20460 S 0.0 0.3
27155 li 20 0 9047876 67320 20460 S 0.0 0.3
可以看到27143的状态是"S"
同理可以验证"high-cpu"对应的native线程是27144,该线程的运行状态正好是R, CPU的消耗也很高
image.png
结论:
Java的IO流的方法是阻塞的指的是内核态的线程的状态而不是JVM的线程的状态
网友评论