下面列出了怎么用java.nio.channels.FileLockInterruptionException的API类实例代码及写法,或者点击链接到github查看源代码。
@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();
}
}
}
/**
* 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
}
}
/**
* 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;
}
/**
* @tests {@link java.nio.channels.FileLockInterruptionException#FileLockInterruptionException()}
*/
public void test_Constructor() {
FileLockInterruptionException e = new FileLockInterruptionException();
assertNull(e.getMessage());
assertNull(e.getLocalizedMessage());
assertNull(e.getCause());
}
/**
* @tests serialization/deserialization compatibility.
*/
public void testSerializationSelf() throws Exception {
SerializationTest.verifySelf(new FileLockInterruptionException());
}
/**
* @tests serialization/deserialization compatibility with RI.
*/
public void testSerializationCompatibility() throws Exception {
SerializationTest.verifyGolden(this,
new FileLockInterruptionException());
}