简体   繁体   English

Java BufferedReader readline阻塞?

[英]Java BufferedReader readline blocking?

I want to make an HTTP request and then get the response as sketched here: 我想发一个HTTP请求,然后按照草图在这里得到响应:

URLConnection c = new URL("http://foo.com").openConnection();
c.setDoOutput(true);

/* write an http request here using a new OutputStreamWriter(c.getOutputStream) */

BufferedReader reader = new BufferedReader(new InputStreamReader(c.getInputStream));
reader.readLine();

But my question is, if the request I send takes a long time before a response is received, what happens in the call reader.readLine() above? 但我的问题是,如果我发送的请求需要很长时间才能收到响应,那么上面的调用reader.readLine()会发生什么? Will this process stay running/runnable on the CPU or will it get taken off the CPU and be notified to wake up and run again when there is IO to be read? 此进程是否会在CPU上保持运行/可运行,还是会从CPU中取出并在有IO被读取时被通知唤醒并再次运行?

If it stays on the CPU, what can be done to make it get off and be notified later? 如果它停留在CPU上,可以做些什么让它下来并在以后得到通知?

What the others have said is correct. 其他人所说的是正确的。 Java's "old I/O" library in java.io contains blocking calls. Java中的Java“旧I / O”库包含阻塞调用。 But they do not busy wait. 但他们并不忙等待。 They are blocking on I/O and the kernel will reschedule them once more I/O is available. 它们阻塞了I / O,内核将在I / O可用时重新安排它们。

I wasn't totally sure, so I tried it out for myself. 我不太确定,所以我亲自试了一下。 Take this sample class: 拿这个样本类:

import java.io.*;

public class Test {

  public static void main(String[] args) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    String line = reader.readLine();
    System.out.println(line);
  }
}

And compile it on the command line. 并在命令行上编译它。 Then run it, but don't type anything. 然后运行它,但不要输入任何内容。 The program should be blocking on input until you type a character, and it will not progress past readline until you type enter. 程序应该在输入时阻塞,直到您键入一个字符,并且在您输入enter之前它不会超过readline。 ps should be able to tell us more details about this process. ps应该能够告诉我们有关此过程的更多详细信息。 Use the a flag to get more detailed information from ps : 使用a标志从ps获取更多详细信息:

japeters@<computer-name>] ps a
  PID   TT  STAT      TIME COMMAND
 3846 s000  S      0:00.16 -zsh
 3992 s000  S+     0:00.40 /usr/bin/java Test

The man page for PS says: PS的手册页说:

state The state is given by a sequence of characters, for example, ``RWNA''. state状态由一系列字符给出,例如“RWNA”。 The first character indicates the run state of the process: 第一个字符表示进程的运行状态:

  • I Marks a process that is idle (sleeping for longer than about 20 seconds). 标记一个空闲的进程(睡眠时间超过约20秒)。
  • R Marks a runnable process. R标志着一个可运行的过程。
  • S Marks a process that is sleeping for less than about 20 seconds. S标记一个睡眠时间少于20秒的过程。

And since I just started the process, S makes sense. 因为我刚开始这个过程,所以S才有意义。 The process is sleeping, awaiting scheduling by the OS. 进程正在休眠,正在等待操作系统的调度。 Indeed, if you check top , you'll notice the process is taking 0% CPU. 实际上,如果你检查top ,你会发现这个过程占用了0%的CPU。

So don't worry about the performance of this call, there's no busy waiting or polling going on: the system is taking care of the I/O events for you and will intelligently handle your process. 因此,不要担心此调用的性能,没有繁忙的等待或轮询:系统正在为您处理I / O事件,并将智能地处理您的过程。

A good OS will have a scheduler that puts the process into a blocked or similar state, and any task switches will not switch to the blocked process. 一个好的操作系统将有一个调度程序,将进程置于阻塞或类似状态,任何任务切换都不会切换到被阻止的进程。

For example, The idle process in windows is what runs when there are no processes ready to run (sleeping, blocked, etc.). 例如,Windows中的空闲进程是在没有进程准备好运行(休眠,阻塞等)时运行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM