Re: Interplatform (interprocess, interlanguage) communication



Roedy Green schrieb:
Let's say you used a simple RandomAccessFile. How could you implement
a busy lock field in the file to indicate the file was busy being
updated? or busy being read? In RAM you have test and set locks to
check a value and set the value in one atomic operation. How could
you simulate that without test and set hardware on the SSD? You can't
very well share a RAM lock between separate jobs.

What do you want, a write lock or a read lock?
Here is a write lock:

Obtain the lock:
raf = new RandomAccessFile(file, "rw");

fo = new FileOutputStream(raf.getFD());
fo.getChannel().lock(0, Long.MAX_VALUE, false);

Release the lock:
fo.close();

raf.close();

Maybe it can be done even simpler, but the above
works for me over process / jvm boundaries. Can
be also used to synchronize jvm with non-jvm code.

Similar code I use to obtain a read lock, via an
FileInputStream and the lock() methods third
argument =true. Currently seems also to work on
Android, but did not yet thoroughly test...

Bye

(*)
http://docs.oracle.com/javase/1.4.2/docs/api/java/nio/channels/FileChannel.html#lock%28long,%20long,%20boolean%29
.