I'm implementing a quick file server using Java RMI, which has 2 remote methods, one is for reading a file from the server and the other is for writing it to the client.
I want to lock concurrent access to same file. For example, if 10 users concurrently call read() and write() remote methods, specifying the same file: 'foo.txt', I need that the first call to be completely executed before the second call does, and the second call is completely executed before the third call does...
If I use "syncronized" statement in the two RMI methods, I lose efficiency if different users concurrently call these methods, specifying different files.
On the other hand, I can't use the FileLock class because:
"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."
and in RMI, there is only one process in the same JVM, that executes new threads for each remote call.