Code example for FileChannel

Methods: tryLock

0
    } 
 
    public void test_tryLockLLZ() throws IOException { 
        // It is illegal to request an exclusive lock on a read-only channel 
        try { 
            readOnlyChannel.tryLock(0, 99, false);
            fail("Acquiring exclusive lock on read-only channel should fail");
        } catch (NonWritableChannelException ex) {
            // Expected 
        } 
 
        // It is invalid to request a lock starting before the file start 
        try { 
            readOnlyChannel.tryLock(-99, 0, true);
            fail("Acquiring an illegal lock value should fail.");
        } catch (IllegalArgumentException ex) {
            // expected 
        } 
 
        // Acquire a valid lock 
        FileLock tmpLock = readOnlyChannel.tryLock(0, 10, true);
        assertTrue(tmpLock.isValid());
        tmpLock.release();
 
        // Acquire another valid lock -- and don't release it yet 
        FileLock lock = readOnlyChannel.tryLock(10, 788, true);
        assertTrue(lock.isValid());
 
        // Overlapping locks are illegal 
        try { 
            readOnlyChannel.tryLock(1, 23, true);
            fail("Acquiring an overlapping lock should fail.");
        } catch (OverlappingFileLockException ex) {
            // Expected 
        } 
 
        // Adjacent locks are legal 
        FileLock adjacentLock = readOnlyChannel.tryLock(1, 3, true);
        assertTrue(adjacentLock.isValid());
        adjacentLock.release();
 
        // Release longer lived lock 
        lock.release();
Connect your IDE to all the code out there  Get Codota for Java