protected void releaseLock() throws IOException { storeLockFileLock.release(); storeLockFileLock = null; } }
@Override public void release() throws IOException { base.release(); }
/** * Release the file lock. */ public synchronized void releaseLock() { if (file != null && lock != null) { try { lock.release(); } catch (Exception e) { // ignore } lock = null; } }
/** * Calls {@link #release} for {@code AutoCloseable}. * * @since 1.7 */ public final void close() throws IOException { release(); }
@Override public void close() throws IOException { try { if (fileLock != null) { fileLock.release(); } } finally { if (outputStream != null) { outputStream.close(); } } } }
private void unlockBasePersistDirectory() { try { if (basePersistDirLock != null) { basePersistDirLock.release(); basePersistDirLockChannel.close(); basePersistDirLock = null; } } catch (IOException e) { throw Throwables.propagate(e); } }
public synchronized void release() throws IOException { if (licenses <= 0) { throw new IllegalStateException("Already released all semaphore licenses"); } licenses -= 1; if (licenses <= 0) { if (lock != null) { lock.release(); } channel.close(); } } }
/** * Quietly releases file lock ignoring all possible exceptions. * * @param lock File lock. If it's {@code null} - it's no-op. */ public static void releaseQuiet(@Nullable FileLock lock) { if (lock != null) try { lock.release(); } catch (Exception ignored) { // No-op. } }
FileInputStream in = new FileInputStream(file); try { java.nio.channels.FileLock lock = in.getChannel().lock(); try { Reader reader = new InputStreamReader(in, charset); ... } finally { lock.release(); } } finally { in.close(); }
@LifecycleStop public void stop() throws Exception { parentMonitorExec.shutdown(); synchronized (this) { if (taskLockFileLock != null) { taskLockFileLock.release(); } if (taskLockChannel != null) { taskLockChannel.close(); } } } }
public static void main(String[] args) throws IOException { // trying FileLock mechanics in different processes File file = new File("tmp.lock"); file.createNewFile(); FileChannel channel = new RandomAccessFile(file, "r").getChannel(); boolean shared = true; FileLock lock1 = channel.tryLock(0, Long.MAX_VALUE, shared); System.out.println("locked " + lock1); System.in.read(); System.out.println("release " + lock1); lock1.release(); }
private void reLock() throws IOException { if (lock == null) { return; } try { lock.base.release(); } catch (IOException e) { // ignore } FileLock l2 = channel.tryLock(lock.position(), lock.size(), lock.isShared()); if (l2 == null) { throw new IOException("Re-locking failed"); } lock.base = l2; }
@Override public void run() { try { RandomAccessFile raf = new RandomAccessFile(file, "r"); System.out.println("Getting lock (parallel thread)..."); FileLock lock = raf.getChannel().lock(0, Long.MAX_VALUE, true); System.out.println("Obtained lock (parallel thread): " + lock); lock.release(); } catch (Throwable e) { e.printStackTrace(); } } });
@Override public void run() { try { RandomAccessFile raf = new RandomAccessFile(file, "rw"); System.out.println("Getting lock (parallel thread)..."); FileLock lock = raf.getChannel().lock(); System.out.println("Obtained lock (parallel tread): " + lock); lock.release(); } catch (Throwable e) { e.printStackTrace(); } } });
@Test @RunIf(value = EnhancedOSChecker.class, arguments = {EnhancedOSChecker.WINDOWS}) public void shouldThrowExceptionWhenWorkingDirectoryIsNotGitRepoAndItsUnableToDeleteIt() throws Exception { File fileToBeLocked = new File(workingDir, "file"); RandomAccessFile lockedFile = new RandomAccessFile(fileToBeLocked, "rw"); FileLock lock = lockedFile.getChannel().lock(); try { git.latestModification(workingDir, new TestSubprocessExecutionContext()); fail("Should have failed to check modifications since the file is locked and cannot be removed."); } catch (Exception e) { assertEquals(e.getMessage().trim(), "Failed to delete directory: " + workingDir.getAbsolutePath().trim()); assertEquals(true, fileToBeLocked.exists()); } finally { lock.release(); } }
@Test public void ch_tryLock() throws IOException { chan( false ).tryLock().release(); }