Java的System.out.print()会永远缓冲到println()吗? [英] Will Java's System.out.print() buffer forever until println()?

查看:365
本文介绍了Java的System.out.print()会永远缓冲到println()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我无意中听到了关于 System.out.print()的争论。一个人声称,由于 print()不包括终止 \ n ,它写入的缓冲区将最终填满并开始丢失数据。另一个人声称他们一直在使用 System.out.print()来处理所有Java程序,并且从未遇到过这个问题。

I overheard an argument about System.out.print() today. One person claimed that since print() doesn't including the terminating \n, the buffer it writes to will eventually fill up and start losing data. The other person claimed that they had been using System.out.print() for all their Java programs and had never run into this issue.

第一个人是对的吗?如果stdout已满, System.out.print()是否可以开始阻止或删除数据?是否有导致此问题的代码示例?

Is the first person right? Is it possible for System.out.print() to start blocking or dropping data if stdout is full? Is there an example of code that will cause this?

推荐答案

System.out使用的缓冲区时.print 填满后,输出被写入文件(或连接到程序标准输出流的终端或其他数据目标),从而产生一个空缓冲区。在不关心缓冲区大小的情况下编写输出是正常的用法。您永远不会崩溃或阻止您的程序或丢失数据而不会调用 flush

When the buffer used by System.out.print fills up, the output is written into the file (or terminal or other data target that is connected to the program's standard output stream), resulting in an empty buffer. Writing output without caring about the buffer size is normal usage. You will never crash or block your program or lose data from not calling flush.

您只需要致电<如果您需要立即在程序外部提供数据,请明确code> flush 。例如,如果您的程序与另一个程序来回交换数据,并且您正在向该程序发送请求并等待该程序的回复,则需要调用 flush 发送请求后确保其他程序收到请求。同样,如果你的程序(或它运行的机器)崩溃,那么只保证在你最后一次调用 flush 时的输出被写出来。

You only need to call flush explicitly if you need to make the data available outside your program immediately. For example, if your program is exchanging data back and forth with another program, and you're sending a request to that program and will wait for that program's reply, you need to call flush after sending your request to ensure that the other program receives it. Similarly, if your program (or the machine it runs on) crashes, only the output up to the last time you called flush is guaranteed to have been written out.

如果流设置为自动刷新,则编写换行符(显式或通过 println )与调用一样好冲洗。调用关闭也会调用 flush (这就是为什么 关闭 可以抛出 IOException :它可能必须写出数据而不能,例如因为流连接到整个磁盘上的文件。)

If the stream is set to flush automatically, then writing a newline character (explicitly or through println) is as good as calling flush. Calling close also calls flush (that's why close can throw an IOException: it might have to write data out and not be able to, e.g. because the stream is connected to a file on a full disk).

注意刷新缓冲区可能导致程序阻止,如果 System.out 所连接的流未立即准备好接收数据(例如,当数据通过管道传输到另一个程序时不要立即读取它的输入。由于缓冲区可以随时刷新(因为缓冲区恰好已满),因此使用非空参数调用 print 可能会阻塞。

Note that flushing the buffer may cause the program to block, if the stream that System.out is connected to is not immediately ready to receive the data (e.g. when the data is piped to another program that doesn't read its input immediately). Since the buffer can be flushed at any time (because the buffer happens to be full), any call to print with a non-empty argument is potentially blocking.

有关详细信息,请参阅缓冲流教程 java.io.PrintStream的文档上课

For more information, see the buffered streams tutorial and the documentation of the java.io.PrintStream class.

这篇关于Java的System.out.print()会永远缓冲到println()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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