views:

34

answers:

1

I am trying to use

FileLock lock(long position, long size,boolean shared)

in FileChannel object As per the javadoc it can throw OverlappingFileLockException. When I create a test program with 2 threads lock method seems to be waiting to acquire the lock (both exclusive and non exclusive) But when the number threads increases in the acutal scenario over lapping file lock exception is thrown and processing slows down due the block at File lock table.

What is the best way to acquire lock avoiding the OverlappingFileLockException ?

A: 

Avoid letting your regions overlap. You could also try to set the shared flag to true. But beware, that shared locks are os dependent. So they may not be available on all systems.

ablaeul
Shared flag can be set to true while reading but while writing it has to be exclusive lock. So shared has to be false. This case reading and writing are both being carried out.
nbhat