When a Java program calls System.out.println() or a Scala program calls println() does the thread block?
I'm writing a Scala program with a huge amount of subtasks. Each subtask is executed inside a Future. It is recommended that code inside actors and futures does not block, so that subsequent tasks don't have to wait, too. But I want to print very much on the console.
And if it is a blocking operation: What can I do to optimize performance?
- Should I use a dedicated thread for console output, so that thread is the only one that blocks?
- Any other advices?
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.
It depends. On Windows OS it's a blocking operation and involves a lot of kernel stuff to print something to the console. In UNIX-like OS the operation is buffered so it's not perceived as slow.
I would suggest you go with your buffer approach, and having a separate thread is also a good idea. Or if your output is not as important, you could write it to a file, which is much faster than writing to console.
Yes and no.
System.out
is aPrintStream
which is a synchronized class. So multiple threads writing large amounts toSystem.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.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 toSystem.out
. You will need to use something like aBlockingQueue
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.The
BufferedWriter
will take care of this for you.GzipOutputStream
to compress it on the fly.