System.out.println and System.err.println out of order

后端 未结 7 1139
灰色年华
灰色年华 2020-11-22 03:44

My System.out.println() and System.err.println() calls aren\'t being printed to the console in the order I make them.

public static         


        
7条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 04:27

    If you are using the Eclipse console, there seem to be two different phenomena at work:
    One, as described by @Gemtastic, is the JVMs handling of the streams and the other is the way Eclipse reads these streams, as mentioned by @DraganBozanovic. Since I'm using Eclipse, the elegant flush()-solution posted by @BillK, which only addresses the JVM-issue, is not sufficient.

    I ended up writing myself a helper-class called EclipseTools with the following content (and the required package declaration and imports). It's a bit of a hack but fixes both issues:

    public class EclipseTools {
    
        private static List streams = null;
        private static OutputStream lastStream = null;
    
        private static class FixedStream extends OutputStream {
    
            private final OutputStream target;
    
            public FixedStream(OutputStream originalStream) {
                target = originalStream;
                streams.add(this);
            }
    
            @Override
            public void write(int b) throws IOException {
                if (lastStream!=this) swap();
                target.write(b);
            }
    
            @Override
            public void write(byte[] b) throws IOException {
                if (lastStream!=this) swap();
                target.write(b);
            }
    
            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                if (lastStream!=this) swap();
                target.write(b, off, len);
            }
    
            private void swap() throws IOException {
                if (lastStream!=null) {
                    lastStream.flush();
                    try { Thread.sleep(200); } catch (InterruptedException e) {}
                }
                lastStream = this;
            }
    
            @Override public void close() throws IOException { target.close(); }
            @Override public void flush() throws IOException { target.flush(); }
        }
    
        /**
         * Inserts a 200ms delay into the System.err or System.out OutputStreams
         * every time the output switches from one to the other. This prevents
         * the Eclipse console from showing the output of the two streams out of
         * order. This function only needs to be called once.
         */
        public static void fixConsole() {
            if (streams!=null) return;
            streams = new ArrayList();
            System.setErr(new PrintStream(new FixedStream(System.err)));
            System.setOut(new PrintStream(new FixedStream(System.out)));
        }
    }
    

    To use, just call EclipseTools.fixConsole() once in the beginning of your code.

    Basically, this replaces the two streams System.err and System.out with a custom set of streams that simply forward their data to the original streams, but keep track of which stream was written to last. If the stream that is written to changes, for example a System.err.something(...) followed by a System.out.something(...), it flushes the output of the last stream and waits for 200ms to give the Eclipse console time to complete printing it.

    Note: The 200ms are just a rough initial value. If this code reduces, but does not eliminate the problem for you, increase the delay in Thread.sleep from 200 to something higher until it works. Alternatively, if this delay works but impacts performance of your code (if you alternate streams often), you can try reducing it gradually until you start getting errors.

提交回复
热议问题