我将通过非常慢的ssh连接运行程序。它会减慢或阻止
System.out.println();
大量印刷。因此,如果它在控制台中打印几千兆字节,但我的连接速度很慢 - 会出现未插入的数据? tty内存的大小是多少?如果我暂时失去连接 - 它会不会运行吗?
答案 0 :(得分:1)
没有。 PrintWriter不会等待确认完成。
答案 1 :(得分:0)
它是否会阻止程序,它会有延迟
Java的控制台输出为blocking,因此您的代码可能会阻塞,尤其是在编写大量数据时。
tty内存的大小是多少?
我很确定它取决于你的内核,这个old thread表明它在某个时刻是4096字节:
我查看了内核代码(linux \ drivers \ char \ serial.c),并且有一个名为SERIAL_XMIT_SIZE的#define。起初我想也许我可以改变它,但似乎发送缓冲区实际上固定为一个内存页面(4k)。
如果我暂时失去连接 - 它会不会运行吗?
是的,如果没有人连接到tty,那么它将运行得更快,因为它可以丢弃数据。
也是模拟用例的小型测试应用程序。
Echo.java
import java.io.IOException;
public class Echo {
public static void main(String[] args) throws InterruptedException, IOException {
final byte[] data = new byte[Test.BODY_LENGTH + Test.END_MARKER.length];
int index = 0;
outer: while (true) {
data[index++] = (byte) System.in.read();
final int dataOffset = index - Test.END_MARKER.length;
if (dataOffset < 0) {
continue;
}
for (int i = 0; i < Test.END_MARKER.length; i++) {
if (data[dataOffset + i] != Test.END_MARKER[i]) {
continue outer;
}
}
System.out.print(new String(data, 0, index));
return;
}
}
}
Test.java
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
public class Test {
public static final byte[] END_MARKER = "$TERMINATE$".getBytes();
public static final int BODY_LENGTH = 1024768;
public static void main(String[] args) throws IOException, InterruptedException {
StringBuilder data = new StringBuilder();
for (int i = 0; i < BODY_LENGTH; i++) {
data.append((char) ('a' + ThreadLocalRandom.current().nextInt(('z' - 'a' + 1))));
}
final Process process = new ProcessBuilder("java", Test.class.getPackage().getName() + ".Echo")
.directory(new File("out/production/week 3")) // Change to your output directory
.start();
process.getOutputStream().write(data.toString().getBytes());
process.getOutputStream().write(END_MARKER);
process.getOutputStream().flush();
System.out.println("Written!");
final boolean exitedAfterWroteData = process.waitFor(5, TimeUnit.SECONDS);
System.out.println(exitedAfterWroteData ? "Complete" : "Running"); // Will print running after 5 seconds
int read = 0;
while (process.getInputStream().read() > -1) {
read++;
}
if (read != data.toString().getBytes().length + END_MARKER.length) {
throw new IllegalStateException("Expected echo to print exactly " + BODY_LENGTH + END_MARKER.length + " symbols!");
}
final boolean exitedAfterWeReadData = process.waitFor(50, TimeUnit.MILLISECONDS);
System.out.println(exitedAfterWeReadData ? "Complete" : "Running"); // Will print complete after a few milliseconds
}
}