美文网首页
Java IO流阻塞现状理解

Java IO流阻塞现状理解

作者: lixwcqs | 来源:发表于2020-07-23 09:16 被阅读0次

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的线程的状态

相关文章

  • Java IO流阻塞现状理解

    Java线程的状态有五种.其中IO流操作一些接口是阻塞操作的例如 看scanner.next()这行代码可以一直跟...

  • java IO、NIO、AIO

    IO流(同步、阻塞) 、 NIO(同步、非阻塞) 、 NIO2(异步、非阻塞) 概述在我们学习Java的IO流之前...

  • IO和NIO区别

    IO是面向流的,NIO是面向缓冲区的;Java IO的各种流是阻塞的,Java NIO的非阻塞模式;Java NI...

  • 40.IO和NIO区别

    一.IO是面向流的,NIO是面向缓冲区的。 二.IO的各种流是阻塞的,NIO是非阻塞模式。 三.Java NIO的...

  • Java中的IO模型

    Java中的IO模型 Java中的IO模型有四种: 同步阻塞IO 同步非阻塞IO IO多路复用 异步IO 其中IO...

  • Java AIO基础

    Java AIO(异步IO)特性是在Java7引入的。 [TOC] 同步异步、阻塞非阻塞的理解 同步和异步 同步和...

  • 详解IO复用模型select,poll,epoll机制

    在Java中,主要有三种IO模型,分别是阻塞IO(BIO)、非阻塞IO(NIO)和 异步IO(AIO)。Java中...

  • Java NIO 和 IO 之间的主要差别

    NIO 和 IO 之间的主要差别 IO NIO 面向流 面向缓冲 阻塞IO 非阻塞IO 无 ...

  • JAVA NIO

    Java NIO指JDK 1.4中提供的新IO,可以理解为非阻塞IO(non-blocking IO),为所有的原...

  • Java Socket IO演进(一)-BIO/NIO/AIO

    1. 概览 Java中主要有三种IO模型,分别是同步阻塞IO(BIO)、同步非阻塞IO(NIO)、异步非阻塞IO(...

网友评论

      本文标题:Java IO流阻塞现状理解

      本文链接:https://www.haomeiwen.com/subject/lrlclktx.html