0

clojure 1.10.1

println at least outputs in a sub-thread but printf doesn't at all.

(.start (Thread. #(println "Hi !"))) ; => Hi

(.start (Thread. #(printf "%s\n" "Hi"))) 

This question seemingly dealt with println synchronization. Any advice on printf?

@EDIT

Above, both println and printf output on the clj REPL. println outputs but printf doesn't as standalone program.

sof
  • 9,113
  • 16
  • 57
  • 83
  • CNR -- works just fine when I run it. – Charles Duffy Apr 27 '20 at 15:42
  • What's the context? Is stdout pointing to a TTY or to a non-TTY device? (That modifies the default buffering behavior). – Charles Duffy Apr 27 '20 at 15:43
  • It's [tty](https://unix.stackexchange.com/questions/4126/what-is-the-exact-difference-between-a-terminal-a-shell-a-tty-and-a-con) if no mistaken. – sof Apr 27 '20 at 15:51
  • Then, once again, I simply can't reproduce this. Doesn't happen in the ideone sandbox either; see https://ideone.com/PkEVsh – Charles Duffy Apr 27 '20 at 15:55
  • I saw you called *Thread/sleep* afterwords, then *printf* outputs. – sof Apr 27 '20 at 16:01
  • If I didn't call `Thread/sleep`, the child thread could be killed because the whole program exited before it finished starting up and was able to execute any code. (Or at least, I'd need to worry about whether the child thread was marked as a "daemon thread" to prevent that). It's nothing specific to `printf` or `println`. That's not a problem at a REPL, because the REPL won't automatically exit unless its stdin is closed. – Charles Duffy Apr 27 '20 at 16:08
  • By default Java starts a [non-daemon](https://groups.google.com/forum/#!topic/clojure/Ti0-mpNnVxk) child thread, the parent need wait till the child completes. – sof Apr 27 '20 at 16:13
  • ...anyhow, what does that have to do with this question, and my observation above that it isn't reproducible? – Charles Duffy Apr 27 '20 at 16:14
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/212632/discussion-between-charles-duffy-and-sof). – Charles Duffy Apr 27 '20 at 16:15

1 Answers1

0

For the standalone program, println prints to the standard output, which is a PrintWriter that flushes an internal buffer after printing. From the JavaDoc for PrintStream:

[...] Optionally, a PrintStream can be created so as to flush automatically; this means that the flush method is automatically invoked after a byte array is written, one of the println methods is invoked, or a newline character or byte ('\n') is written.

printf does not do the flushing, so (unless it's done by the REPL) there's no forcing function for it to be printed before the program ends. If you want to flush it manually, you can always do (.flush System/out) or (println) yourself.

Edit: you can see it in the sources of the JDK: println() calls a private method called newLine() that does the flushing:

// ... snip
            synchronized (this) {
                ensureOpen();
                textOut.newLine();
                textOut.flushBuffer();
                charOut.flushBuffer();
                if (autoFlush)
                    out.flush();
Denis Fuenzalida
  • 3,271
  • 1
  • 17
  • 22
  • The test appended *\n* to *printf*. Using *(.printf (System/out) ...)* also printed out. – sof May 01 '20 at 13:30