简体   繁体   中英

replacing multiple threads writing to outputstream with one writer thread (Java)

I have an existing app where multiple threads are writing via an outputstream (and synchronizing on it). I am wondering whether switching over to a single writer principle for writing to the outputstream might be more efficient (I am just doing this for academic reasons as I do not believe this will impact the app performance that much though I could be wrong). For now, I am only thinking of using a blockingqueue or a ring buffer for all current writers to write to and one writer thread (consumer for the queue/ringbuffer) to write to the output stream/channel.
Any thoughts on this or helpful suggestions would be quite welcome.

If you have enough free capacity on your system (idle CPU time, available IO time) then adding a separate thread to do the logging IO only will noticeably improve the performance of the overall system.

Adding a Queue from the java.util.concurrent package with a bunch of threads that add to the queue, and a logging thread that reads from the queue and writes out the output will be a good solution.

On the other hand, if you have a fully utilized system then the queue will either become too large, or the overhead will become noticable, and the benefits of the logging thread will become negligible.

In most cases I have seen a seperate logging thread is beneficial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM