Java BufferedReader readline阻塞? [英] Java BufferedReader readline blocking?

查看:2183
本文介绍了Java BufferedReader readline阻塞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发出一个HTTP请求,然后按照这里的草图获取响应:

I want to make an HTTP request and then get the response as sketched here:

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();

但我的问题是,如果我发送的请求需要很长时间才能收到回复,那么发生在上面的调用reader.readLine()中?这个过程是继续在CPU上运行/运行还是从CPU中取出并在有IO被读取时被通知唤醒并再次运行?

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? 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上,可以做些什么让它下来并在以后通知?

If it stays on the CPU, what can be done to make it get off and be notified later?

推荐答案

其他人所说的是正确的。 Java中的Java旧I / O库包含阻塞调用。但他们不忙等待。它们阻塞了I / O,内核会在I / O可用后重新安排它们。

What the others have said is correct. Java's "old I/O" library in java.io contains blocking calls. 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 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);
  }
}

并在命令行上编译它。然后运行它,但不要输入任何内容。程序应该在输入时阻塞,直到您键入一个字符,并且在您输入enter之前它不会超过readline。 ps 应该能够告诉我们有关此过程的更多详细信息。使用 a 标志从 ps 获取更多详细信息:

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. ps should be able to tell us more details about this process. Use the a flag to get more detailed information from 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

PS的手册页说:


state 状态由
字符序列给出,例如
`` RWNA'。第一个字符
表示
流程的运行状态:

state The state is given by a sequence of characters, for example, ``RWNA''. The first character indicates the run state of the process:


  • 标记一个空闲的进程(睡眠时间超过约
    20秒)。

  • R 标记可运行的进程。

  • S 标记正在休眠的流程少于约20
    秒。

  • I Marks a process that is idle (sleeping for longer than about 20 seconds).
  • R Marks a runnable process.
  • S Marks a process that is sleeping for less than about 20 seconds.

由于我刚开始这个过程, S 是有道理的。进程正在休眠,正在等待操作系统的调度。实际上,如果你检查 top ,你会发现这个过程占用了0%的CPU。

And since I just started the process, S makes sense. The process is sleeping, awaiting scheduling by the OS. Indeed, if you check top, you'll notice the process is taking 0% CPU.

所以不要担心这个电话的性能,没有忙碌的等待或轮询:系统正在为你处理I / O事件,并会智能地处理你的过程。

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.

这篇关于Java BufferedReader readline阻塞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆