Is the console issuing a lock operation?

When a Java program calls System.out.println () or a Scala program calls println (), is a thread block executed?

I am writing a Scala program with a huge number of subtasks. Each subtask is performed inside the Future. It is recommended that the code inside participants and futures not be blocked, so that subsequent tasks should also not wait. But I want to print on the console.

And if it's a lock operation: what can I do to optimize performance?

  • Should I use a dedicated thread to output the console so that the thread is the only one that blocks?
  • Any other tips?

Of course, I could try to reduce the output or collect some result in a StringBuilder and print it together in a batch, which reduces the number of output operations.

+8
java multithreading scala nonblocking blocking
source share
2 answers

When a Java program calls System.out.println () or a Scala program calls println (), is a thread block executed?

Yes and no. System.out is a PrintStream , which is a synchronized class. Thus, multiple threads writing large amounts to System.out will probably block each other. However, if the thread receives a lock, regardless of whether the IO is blocking the thread, it depends on the architecture. If you write a large amount of input-output, which overloads the capacity of the main equipment, recording will be blocked. In addition, creating a large number of small records (as opposed to buffering) will also slow down the flow.

Should I use a dedicated thread to output the console so that the thread is the only one that blocks?

Great idea, yes. Then this thread could write through one BufferedWriter or some kind of log4j or another logging package, which would be much more efficient compared to System.out . You will need to use something like BlockingQueue for a message queue that is synchronous, but IO will never block this queue unless you create messages faster than the IO channel can save them.

Of course, I could try to reduce the output or collect some result in a StringBuilder and print it together in a batch, which reduces the number of output operations.

BufferedWriter will take care of this for you.

Any other tips?

  • As already mentioned, use the best logging package or single-threaded writer.
  • Logging to another physical disk with higher I / O bandwidth.
  • Go to the file system or memory hardware to increase I / O throughput. SSD ++.
  • Send it over the network to another block to make the actual persistent field.
  • Use GzipOutputStream to compress it on the fly.
+12
source share

It depends on . On Windows, this is a blocking operation and includes many kernel materials for printing anything on the console. On a UNIX-like OS, the operation is buffered, so it is not perceived as slow.

I would advise you to go with your approach to buffers, and having a separate thread is also a good idea. Or if your output is not so important, you can write it to a file, which is much faster than writing to the console.

+4
source share

All Articles