FileBasedLock implements Lock, java.io.Closeable { @Override @SuppressFBWarnings("MDM_WAIT_WITHOUT_TIMEOUT") public void lock() { jvmLock.lock(); if (jvmLock.getHoldCount() > 1) { return; } try { fileLock = file.getChannel().lock(); writeHolderInfo(); } catch (IOException ex) { jvmLock.unlock(); throw new LockRuntimeException(ex); } catch (Throwable ex) { jvmLock.unlock(); throw ex; } } private  FileBasedLock(final File lockFile, final FileAttribute<?>... fileAttributes); static Set<PosixFilePermission> extractPosixPermissions(final FileAttribute<?>[] fileAttributes); static FileBasedLock getLock(final File lockFile, final FileAttribute<?>... fileAttributes); static FileBasedLock getLock(final File lockFile); @Override @SuppressFBWarnings("MDM_WAIT_WITHOUT_TIMEOUT") void lock(); @Override @SuppressFBWarnings({"MDM_WAIT_WITHOUT_TIMEOUT", "EXS_EXCEPTION_SOFTENING_HAS_CHECKED", "MDM_THREAD_YIELD"}) void lockInterruptibly(); @Override @SuppressFBWarnings("MDM_THREAD_FAIRNESS") boolean tryLock(); @SuppressFBWarnings({"EXS_EXCEPTION_SOFTENING_HAS_CHECKED", "MDM_THREAD_YIELD"}) @Override boolean tryLock(final long time, final TimeUnit unit); @Override void unlock(); @Override Condition newCondition(); @Override @WillClose void close(); static String getContextInfo(); @Override String toString(); int getLocalHoldCount(); @SuppressFBWarnings("MDM_LOCK_ISLOCKED") boolean isHeldByCurrentThread(); static final FileAttribute<?>[] NO_FILE_ATTRS; }
@Test(expected = OverlappingFileLockException.class) @SuppressFBWarnings("PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS") public void testFileLock() throws IOException { File tmp = File.createTempFile("test", ".lock"); tmp.deleteOnExit(); RandomAccessFile file = new RandomAccessFile(tmp, "rws"); FileChannel channel = file.getChannel(); FileLock lock = channel.lock(); FileLock lock1 = channel.lock(); Assert.fail(); lock1.release(); lock.release(); }