2017-04-20 66 views
0

我将通过非常慢的ssh连接运行程序。它是否会减慢或阻止印刷的大负荷上的Java的System.out.println();它会阻止它的tty会有延迟的程序

System.out.println(); 

。所以,如果它将几千兆字节直接打印到控制台中,但是我的连接速度很慢 - 将出现未被拖动的数据? tty内存大小是多少?如果我暂时失去联系 - 它会继续运行吗?

+0

这个问题太宽泛了,你可以指定更像给人的实例或者有关数据类型讲解和你究竟会尝试吗?数据类型为 –

+0

? PrintStream只打印字符串?这个问题不是关于java,而是更多关于TTY和控制台。 – UPvoter

回答

0

它会阻止程序它的tty会有延迟

的Java控制台输出blocking,所以可能你的代码可能会阻止,尤其是当你写了很多数据。

tty内存大小是多少?

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

我在内核代码看(Linux的\ DRIVERS \字符\ 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 
    } 
} 
相关问题