Java线程发生IO阻塞时的线程状态

在我的 https://www.yuque.com/infuq/default/dsag1h 文章中,对Java线程的状态进行了描述,如下图所示

图片.png

此篇文章,我们验证下,当线程进行socket.read()时,线程的状态是RUNNABLE

实验涉及三个文件, Server.java, Client.java, BusinessHandler.java

package com.infuq.bio;

import com.alibaba.druid.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.time.LocalDate;

public class BusinessHandler implements Runnable {

    private Socket socket;

    public BusinessHandler(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {

        BufferedReader reader = null;
        PrintWriter writer = null;
        try {
            reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            writer = new PrintWriter(this.socket.getOutputStream(), true);

            while (true) {
                System.out.println("准备接收客户端数据");
                String body = reader.readLine();
                if (StringUtils.isEmpty(body)) {
                    break;
                }
                System.out.println("接收客户端数据: " + body);

                writer.println(body + " = " + LocalDate.now());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
                if (writer != null) {
                    writer.close();
                }
                if (this.socket != null) {
                    this.socket.close();
                    this.socket = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

package com.infuq.bio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    private static final int LISTEN_PORT = 8080;
    private static int index = 1;

    public static void main(String[] args) throws Exception {
        ServerSocket server = new ServerSocket(LISTEN_PORT);

        while (true) {
            Socket socket = server.accept();

            BusinessHandler serverHandler = new BusinessHandler(socket);
            new Thread(new BusinessHandler(socket), "BusinessHandlerThread-" + (index++)).start();
        }
    }
}
package com.infuq.bio;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Random;

public class Client {

    private static int PORT = 8080;
    private static String HOST = "127.0.0.1";

    public static void main(String[] args) {

        request();

    }

    private static void request() {
        Socket socket = null;
        BufferedReader reader = null;
        PrintWriter writer = null;
        try {
            socket = new Socket(HOST, PORT);

            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            writer = new PrintWriter(socket.getOutputStream(), true);

            String[] operators = {"+", "-", "*", "/"};
            Random random = new Random(System.currentTimeMillis());
            String expression = random.nextInt(10) + operators[random.nextInt(4)] + (random.nextInt(10) + 1);
            writer.println(expression);

            System.out.println("接收服务端数据: " + reader.readLine());
            // 暂时让客户端不退出
            Thread.sleep(3600 * 1000);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
                if (writer != null) {
                    writer.close();
                }
                if (socket != null) {
                    socket.close();
                    socket = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

目录结构如下图所示

图片.png

分别进行编译
[infuq: bio]# javac -d . Client.java
[infuq: bio]# javac -d . BusinessHandler.java
[infuq: bio]# javac -d . Server.java

编译后如下图


图片.png

首先执行服务端
[infuq: bio]# strace -ff -o out java com.infuq.bio.Server

strace命令可以用于追踪系统调用

再执行客户端
[infuq: bio]# java com.infuq.bio.Client

通过jps 查看服务端的进程号, 进程号1515


图片.png

通过jstack 1515 > 1515.txt 导出线程堆栈信息
查看堆栈信息


图片.png

可以得知,服务端线程在处理IO时阻塞在socket的read方法,线程状态是RUNNABLE.
它的nid=ox617,也就是十进制1559, 我们之前执行服务端时使用的strace会创建出很多以线程ID命名的文件,如下图

图片.png

通过tail -f out.1559查看

图片.png

线程阻塞在系统调用recvfrom方法, 也就是上面我们所说的socket的read方法

而且如果你通过ps -Lf 1515, 那么你看到的线程1559的状态是Sleep状态, 也就是说,对于socket进行read时,如果发生阻塞,那么对应的Java线程的状态是RUNNABLE,而对应的底层操作系统的线程的状态是Sleep, Java线程的状态本身和底层操作系统线程的状态不是一一对等的

你可能感兴趣的:(Java线程发生IO阻塞时的线程状态)