This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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 ?
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.
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
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.