Java Code Examples for java.nio.channels.FileLockInterruptionException

The following examples show how to use java.nio.channels.FileLockInterruptionException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: jimfs   Source File: JimfsFileChannel.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public FileLock lock(long position, long size, boolean shared) throws IOException {
  checkLockArguments(position, size, shared);

  // lock is interruptible
  boolean completed = false;
  try {
    begin();
    completed = true;
    return new FakeFileLock(this, position, size, shared);
  } finally {
    try {
      end(completed);
    } catch (ClosedByInterruptException e) {
      throw new FileLockInterruptionException();
    }
  }
}
 
Example 2
Source Project: jimfs   Source File: JimfsFileChannelTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Asserts that when the given operation is run on an interrupted thread, {@code
 * ClosedByInterruptException} is thrown, the channel is closed and the thread is no longer
 * interrupted.
 */
private static void assertClosedByInterrupt(FileChannelMethod method) throws IOException {
  FileChannel channel = channel(regularFile(10), READ, WRITE);
  Thread.currentThread().interrupt();
  try {
    method.call(channel);
    fail(
        "expected the method to throw ClosedByInterruptException or "
            + "FileLockInterruptionException");
  } catch (ClosedByInterruptException | FileLockInterruptionException expected) {
    assertFalse("expected the channel to be closed", channel.isOpen());
    assertTrue("expected the thread to still be interrupted", Thread.interrupted());
  } finally {
    Thread.interrupted(); // ensure the thread isn't interrupted when this method returns
  }
}
 
Example 3
Source Project: embedded-cassandra   Source File: FileLock.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Acquires an exclusive lock on the file.
 *
 * @param timeout the maximum time to wait
 * @param timeUnit the time unit of the {@code timeout} argument
 * @return {@code true} if lock has been acquired otherwise {@code false}
 * @throws FileLockInterruptionException If the invoking thread is interrupted while blocked in this method
 * @throws IOException If some other I/O error occurs
 */
public boolean tryLock(long timeout, TimeUnit timeUnit) throws FileLockInterruptionException, IOException {
	Objects.requireNonNull(timeUnit, "'timeUnit' must not be null");
	java.nio.channels.FileLock fileLock = this.locks.get(Thread.currentThread());
	if (fileLock != null && fileLock.isValid()) {
		return true;
	}
	long startTime = System.nanoTime();
	long rem = timeUnit.toNanos(timeout);
	do {
		fileLock = lock(this.fileChannel);
		if (fileLock != null) {
			this.locks.put(Thread.currentThread(), fileLock);
			return true;
		}
		if (rem > 0) {
			try {
				Thread.sleep(Math.min(TimeUnit.NANOSECONDS.toMillis(rem) + 1, 100));
			}
			catch (InterruptedException ex) {
				throw new FileLockInterruptionException();
			}
		}
		rem = timeUnit.toNanos(timeout) - (System.nanoTime() - startTime);
	} while (rem > 0);
	return false;
}
 
Example 4
Source Project: j2objc   Source File: FileLockInterruptionExceptionTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @tests {@link java.nio.channels.FileLockInterruptionException#FileLockInterruptionException()}
 */
public void test_Constructor() {
    FileLockInterruptionException e = new FileLockInterruptionException();
    assertNull(e.getMessage());
    assertNull(e.getLocalizedMessage());
    assertNull(e.getCause());
}
 
Example 5
Source Project: j2objc   Source File: FileLockInterruptionExceptionTest.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @tests serialization/deserialization compatibility.
 */
public void testSerializationSelf() throws Exception {

    SerializationTest.verifySelf(new FileLockInterruptionException());
}
 
Example 6
Source Project: j2objc   Source File: FileLockInterruptionExceptionTest.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @tests serialization/deserialization compatibility with RI.
 */
public void testSerializationCompatibility() throws Exception {

    SerializationTest.verifyGolden(this,
            new FileLockInterruptionException());
}