wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes Reading and writing by concurrent threads from a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Reading and writing by concurrent threads from a file " Watch "Reading and writing by concurrent threads from a file " New topic
Author

Reading and writing by concurrent threads from a file

Raees Uzhunnan
Ranch Hand

Joined: Aug 15, 2002
Posts: 126
I am looking for a solution for the below problem.

I want to have a thread that writes to a file and a thread that reads from the same file. Is this possible in java ? Can I ensure that data written by the writer thread will be seen by the reader thread ?

I was thinking of using RandomAccessFile ,FileChannel and FileLock classes for this purpose. Does any one has any suggestions, comments or code


Sun Certified Enterprise Architect
Java Technology Blog
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

The reader and writer are in the same JVM or different ones?

(yanked from javadocs)
File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine.


If they are in the same JVM, "i think" it is better to have a Producer-Consumer using a queue rather than a file. (This queue can be persisted for crash recovery/reporting purposes)

If they are on different JVMs then the FileLock approach should serve your purpose. Probably, the reader would have to know the record/point from where it has to read after a writer has finished writing (if the reader does not erases the data while reading, in that cases a reader can always starts from the start of the file.)
Alternatively, writer can create a fresh file everytime, renaming the old one. Both reader and writer take lock on an exclusive lock file and not the file on which the data is written.

Hope this helps.


apigee, a better way to API!
Raees Uzhunnan
Ranch Hand

Joined: Aug 15, 2002
Posts: 126
My question is about same JVM , Can I open two FileChannels on same file to do exclusively writing and exclusively reading ? the position to read and write can be managed externally. I am looking to avoid the amount of seeks I have to do on large file if I am reading and writing in a FIFO order
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Yes, you can open two channels on the same file simultaneously. In order to read/write meaningful data you would have to co-ordinate between the writer and reader though.
In fact you can use a FileOutputStream opened in an append mode for writing and FileInputStream for reading. You can initialize the channel position of the reader to the initial size of the file and then let the write notify when it is done with writing. Since FileOutputStream is opened in an append mode, it will always move to the end of file before writing.
Raees Uzhunnan
Ranch Hand

Joined: Aug 15, 2002
Posts: 126
Looks like thats what I will do

Thanks,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading and writing by concurrent threads from a file