2017-04-20 67 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 
    } 
} 
相關問題