Java concurrent writes from multiple threads to a single text file?

I have a multi-threaded Java 7 program (a jar file) which uses JDBC to perform work (it uses a fixed thread pool).

The program works fine and it logs things as it progresses to the command shell console window (System.out.printf()) from multiple concurrent threads.

In addition to the console output I also need to add the ability for this program to write to a single plain ASCII text log file - from multiple threads.

The volume of output is low, the file will be relatively small as its a log file, not a data file.

Can you please suggest a good and relatively simple design/approach to get this done using Java 7 features (I dont have Java 8 yet)?

Any code samples would also be appreciated.

thank you very much

EDIT:

I forgot to add: in Java 7 using Files.newOutputStream() static factory method is stated to be thread safe - according to official Java documentation. Is this the simplest option to write a single shared text log file from multiple threads?

Jon Skeet
people
quotationmark

Given that you've said the volume of output is low, the simplest option would probably be to just write a thread-safe writer which uses synchronization to make sure that only one thread can actually write to the file at a time.

If you don't want threads to block each other, you could have a single thread dedicated to the writing, using a BlockingQueue - threads add write jobs (in whatever form they need to - probably just as strings) to the queue, and the single thread takes the values off the queue and writes them to the file.

Either way, it would be worth abstracting out the details behind a class dedicated for this purpose (ideally implementing an interface for testability and flexibility reasons). That way you can change the actual underlying implementation later on - for example, starting off with the synchronized approach and moving to the producer/consumer queue later if you need to.

people

See more on this question at Stackoverflow