Writing a file using multiple threads

前端 未结 3 1228
面向向阳花
面向向阳花 2020-11-30 04:16

I am trying to write a single huge file in Java using multiple threads.

I have tried both FileWriter and bufferedWriter classes in Java.

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 05:06

    Instead of having a synchronized methods, the better solution would be to have a threadpool with single thread backed by a blocking queue. The message application would be writing will be pushed to blocking queue. The log writer thread would continue to read from blocking queue (will be blocked in case queue is empty) and would continue to write it to single file.

提交回复
热议问题