我将通过非常慢的SSH连接运行程序。它会减慢还是阻止
System.out.println();
大量打印。因此,如果它直接将千兆字节打印到控制台中,但是我的连接速度很慢 - 在哪里出现了无人驾驶数据?TTY内存的大小是多少?如果我会失去一段时间的连接 - 它将运行吗?
no。printwriter不会等待确认完成。
它将阻止其TTY具有延迟
的程序
Java的控制台输出正在阻止,因此您的代码可能会阻止,尤其是在编写大量数据时。
TTY内存的大小是多少?
我很确定它取决于您的内核,这个旧线程表明它是4096字节:
我已经查看了内核代码(Linux drivers char serial.c(,并且有一个#define称为serial_xmit_size。起初我以为也许可以改变它,但是似乎发射缓冲区实际上是固定为存储页面(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
}
}