When a Java program calls System.out.println() or a Scala program calls println() does the thread block?
Yes and no. System.out
is a PrintStream
which is a synchronized class. So multiple threads writing large amounts to System.out
will block each other for sure. Once a thread gets the lock however, whether or not the IO will block the thread is architecture dependent. If you write a large amount of IO that overwhelms the capacity of the underlying hardware then the write will block. Also, making a lot of small writes (as opposed to buffered), will slow the thread as well.
Should I use a dedicated thread for console output, so that thread is the only one that blocks?
Excellent idea, yes. Then this thread could write through a single BufferedWriter
or some sort of log4j or other logging package which would be a lot more performant compared to System.out
. You will need to use something like a BlockingQueue
to queue up the messages which is synchronous but the IO will never block this queue unless your are producing messages faster than the IO channel can persist them.
Of course I could try to reduce the amount of output or collect some output in a StringBuilder and print it together in a batch, which reduces the number of output operations.
The BufferedWriter
will take care of this for you.
Any other advices?
- As mentioned, use a better logging package or a single-threaded writer.
- Write logs to a different physical disk that has more IO bandwidth.
- Switch to a memory file-system or hardware to increase your IO bandwidth. SSD++.
- Send it via the network to another box to do the actual persisting off box.
- Use a
GzipOutputStream
to compress it on the fly.