Java 的 System.out.println();它会阻止程序吗 tty 会有延迟

Java's System.out.println(); will it block the program it tty will have latency

我将运行 程序通过非常慢的 ssh 连接。它会减慢还是阻塞

System.out.println();

大量打印。因此,如果它在控制台中直接打印几千兆字节,但我的连接速度很慢 - 未播放的数据会出现在哪里? tty内存的大小是多少?如果我会暂时失去联系 - 还会 运行 吗?

没有。 PrintWriter 不等待完成确认。

Will it block the program it tty will have latency

Java 的控制台输出是 blocking,因此您的代码可能会阻塞,尤其是当您写入大量数据时。

what is the size of tty memory?

我很确定这取决于你的内核,这个 old thread 表明它在某个时刻是 4096 字节:

I've looked in the kernel code (linux\drivers\char\serial.c) and there is a #define called SERIAL_XMIT_SIZE. At first I thought maybe I could change that but it seems that the transmit buffer is actually fixed to be a memory page (4k).

If I will lose connection for a while - will it run still?

是的,如果没有人连接到 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
    }
}