aspose file tools*
The moose likes Threads and Synchronization and the fly likes Appending contents to one file from multiple threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Appending contents to one file from multiple threads" Watch "Appending contents to one file from multiple threads" New topic
Author

Appending contents to one file from multiple threads

Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
Hi,

I have a web application and all the requests and threads are writing to a single file. What is the best way to handle this. As far my understanding goes, the potential issues could be corruption of data ...
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

The simplest and safest approach would probably be to use a producer/consumer model where N threads insert their requests into a queue and 1 thread reads from that queue and writes to the file. If you use java.util.concurrent.ArrayBlockingQueue or java.util.concurrent.ConcurrentLinkedQueue, you wouldn't even have to handle the synchronization yourself.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Appending contents to one file from multiple threads