Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading and writing by concurrent threads from a file

 
Raees Uzhunnan
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raees Uzhunnan
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like thats what I will do

Thanks,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic