This behavior is reproduced using the flock (2) system call and shell (1).
Since you can reproduce it outside of Java, this sounds like an infrastructure issue. You did not provide too much information about your NFS server or client OS, but one thing I saw causes strange behavior with NFS is the wrong DNS configuration.
Verify that the output of "uname -n" and "hostname" on the client matches your DNS records. Verify that the NFS server resolves DNS correctly.
Like Guntram, I also do not recommend using NFS for this kind of thing. I would use Hazlecast (without a server, instances of dynamic clusters) or ZooKeeper (you need to configure the server).
With Hazlecast, you can do this to get an exclusive cluster lock:
import com.hazelcast.core.Hazelcast; import java.util.concurrent.locks.Lock; Lock lock = Hazelcast.getLock(myLockedObject); lock.lock(); try { // do something here } finally { lock.unlock(); }
It also supports timeouts:
if (lock.tryLock (5000, TimeUnit.MILLISECONDS)) { try { // do some stuff here.. } finally { lock.unlock(); } }
source share