开发者

In Java What is the guaranteed way to get a FileLock from FileChannel while accessing a RandomAccessFile?

开发者 https://www.devze.com 2022-12-30 20:03 出处:网络
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 th

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 t开发者_运维知识库he 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 ?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消