List of usage examples for java.nio.channels FileLock isShared
public final boolean isShared()
From source file:Main.java
public static void main(String[] argv) throws Exception { File file = new File("filename"); FileChannel channel = new RandomAccessFile(file, "rw").getChannel(); FileLock lock = channel.lock(0, Long.MAX_VALUE, true); lock = channel.tryLock(0, Long.MAX_VALUE, true); boolean isShared = lock.isShared(); lock.release();/*from w w w .j a v a2 s .c o m*/ channel.close(); }
From source file:ca.uviccscu.lp.server.main.ShutdownListener.java
@Deprecated public void releaseLocks() { l.trace("Trying to check file locks "); try {/*from w w w . j a v a 2 s. co m*/ Collection c = FileUtils.listFiles(new File(Shared.workingDirectory), null, true); Object[] arr = c.toArray(); for (int i = 0; i < arr.length; i++) { l.trace("Trying lock for: " + ((File) arr[i]).getAbsolutePath()); // Get a file channel for the file File file = ((File) arr[i]); FileChannel channel = new RandomAccessFile(file, "rw").getChannel(); // Use the file channel to create a lock on the file. // This method blocks until it can retrieve the lock. //FileLock lock = channel.lock(); // Try acquiring the lock without blocking. This method returns // null or throws an exception if the file is already locked. FileLock lock = null; try { lock = channel.lock(); } catch (OverlappingFileLockException ex) { l.trace("Lock already exists", ex); } if (lock == null) { l.trace("Lock failed - someone else holds lock"); } else { l.trace("Lock shared: " + lock.isShared() + " Lock valid: " + lock.isValid()); lock.release(); l.trace("Lock release OK"); try { if (!file.delete()) { throw new IOException(); } } catch (Exception e) { l.trace("Delete failed", e); } } } //Thread.sleep(25000); } catch (Exception e) { // File is already locked in this thread or virtual machine l.trace("File lock problem", e); } }