我用 Java use 编写了一个简单的 ECHO 协议服务器Thread。
就这么简单:
ServerSocket serverSocket = new ServerSocket(7000, 0, InetAddress.getByName("127.0.0.1"));
while (true) {
Socket socket = serverSocket.accept();
new Thread(() -> {
try (Socket s = socket;
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()))) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
out.write(inputLine + '\n');
out.flush();
}
} catch (IOException e) {
}
}).start();
}
线程正在等待客户端的输入inputLine = in.readLine()。但是我发现所有工作线程都处于RUNNABLE状态jstack,我认为它们可能处于WAITING状态。
线程在等待数据到达时被阻塞,但为什么在这里呢RUNNABLE?
这是jstack输出:
"Thread-0" #10 prio=5 os_prio=31 tid=0x00007f9f8282e800 nid=0x1307 runnable [0x000070000623a000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:170)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
- locked <0x000000076b167a18> (a java.io.InputStreamReader)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
- locked <0x000000076b167a18> (a java.io.InputStreamReader)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at EchoServer3.lambda$main$0(EchoServer3.java:20)
宝慕林4294392
相关分类