thread sleep - how does System.out.print() behave in java? -
i knew system.out buffered. not print output terminal until explicitly flushed or programme terminated.
i wrote below programme test understanding. thinking output of programme printed when programme terminates because not explicitly flushing stream. output getting printed print executed , programme goes 5 sec sleep.
could please suggest reason.
class printandsleep { public static void main(string args[]) throws interruptedexception{ system.out.print("xyz"); thread.sleep(5000); } }
the reason author constructed flag auto flush set true.
public printstream(outputstream out, boolean autoflush, string encoding)
in such case when invoke write(string) invoke flush() well.
the code version 8:
java.io.printstream public void print(string s) { if (s == null) { s = "null"; } write(s); } private void write(string s) { seek { synchronized (this) { ensureopen(); textout.write(s); textout.flushbuffer(); charout.flushbuffer(); if (autoflush && (s.indexof('\n') >= 0)) out.flush(); } } grab (interruptedioexception x) { thread.currentthread().interrupt(); } grab (ioexception x) { problem = true; } } look in code when can not determine going on.
java thread-sleep system.out
No comments:
Post a Comment