public static <T> T getUnderFileLock(final Path lockFilePath, final Supplier<T> work) {

        final Instant start = Instant.now();
        LOGGER.debug("Using lock file {}", lockFilePath.toAbsolutePath());

        try (final FileOutputStream fileOutputStream = new FileOutputStream(lockFilePath.toFile());
                final FileChannel channel = fileOutputStream.getChannel()) {
            channel.lock();

            LOGGER.debug(() -> LogUtil.message("Waited {} for lock",
                    Duration.between(start, Instant.now())));

            // Do the work while under the lock
            T result = work.get();

            LOGGER.debug("Work complete, releasing lock");
            return result;
        } catch (IOException e) {
            throw new RuntimeException("Error opening lock file " + lockFilePath.toAbsolutePath(), e);
        }
    }
Obtains a write lock on lockFilePath then runs the work. Creates lockFilePath
if it doesn't exist. Will block if another thread/jvm holds a lock on the same
file.
stroom-util/src/main/java/stroom/util/io/FileUtil.java