### Question:
LockFile implements Closeable { public static LockFile lock(Path lockFile) throws IOException { try { lockMap.computeIfAbsent(lockFile, key -> new ReentrantLock()).lockInterruptibly(); } catch (InterruptedException ex) { throw new IOException("Interrupted while trying to acquire lock", ex); } Files.createDirectories(lockFile.getParent()); FileOutputStream outputStream = new FileOutputStream(lockFile.toFile()); FileLock fileLock = null; try { fileLock = outputStream.getChannel().lock(); return new LockFile(lockFile, fileLock, outputStream); } finally { if (fileLock == null) { outputStream.close(); } } } private  LockFile(Path lockFile, FileLock fileLock, OutputStream outputStream); static LockFile lock(Path lockFile); @Override void close();  }### Answer:
@Test public void testLockAndRelease() throws InterruptedException { AtomicInteger atomicInt = new AtomicInteger(0); Runnable procedure = () -> { try (LockFile ignored = LockFile.lock(temporaryFolder.getRoot().toPath().resolve("testLock"))) { Assert.assertTrue(Files.exists(temporaryFolder.getRoot().toPath().resolve("testLock"))); int valueBeforeSleep = atomicInt.intValue(); Thread.sleep(100); atomicInt.set(valueBeforeSleep + 1); } catch (InterruptedException | IOException ex) { throw new AssertionError(ex); } }; Thread thread = new Thread(procedure); thread.start(); procedure.run(); thread.join(); Assert.assertEquals(2, atomicInt.intValue()); }