下面列出了怎么用java.nio.channels.InterruptedByTimeoutException的API类实例代码及写法,或者点击链接到github查看源代码。
ReadOperation startRead(
RegisteredSocket socket,
ByteBufferSet buffer,
long timeout, TimeUnit unit,
LongConsumer onSuccess, Consumer<Throwable> onFailure)
throws ReadPendingException {
checkTerminated();
Util.assertTrue(buffer.hasRemaining());
waitForSocketRegistration(socket);
ReadOperation op;
socket.readLock.lock();
try {
if (socket.readOperation != null) {
throw new ReadPendingException();
}
op = new ReadOperation(buffer, onSuccess, onFailure);
/*
* we do not try to outsmart the TLS state machine and register for both IO operations for each new socket
* operation
*/
socket.pendingOps.set(SelectionKey.OP_WRITE | SelectionKey.OP_READ);
if (timeout != 0) {
op.timeoutFuture = timeoutExecutor.schedule(() -> {
boolean success = doCancelRead(socket, op);
if (success) {
op.onFailure.accept(new InterruptedByTimeoutException());
}
}, timeout, unit);
}
socket.readOperation = op;
} finally {
socket.readLock.unlock();
}
selector.wakeup();
startedReads.increment();
currentReads.increment();
return op;
}
WriteOperation startWrite(
RegisteredSocket socket,
ByteBufferSet buffer,
long timeout, TimeUnit unit,
LongConsumer onSuccess, Consumer<Throwable> onFailure)
throws WritePendingException {
checkTerminated();
Util.assertTrue(buffer.hasRemaining());
waitForSocketRegistration(socket);
WriteOperation op;
socket.writeLock.lock();
try {
if (socket.writeOperation != null) {
throw new WritePendingException();
}
op = new WriteOperation(buffer, onSuccess, onFailure);
/*
* we do not try to outsmart the TLS state machine and register for both IO operations for each new socket
* operation
*/
socket.pendingOps.set(SelectionKey.OP_WRITE | SelectionKey.OP_READ);
if (timeout != 0) {
op.timeoutFuture = timeoutExecutor.schedule(() -> {
boolean success = doCancelWrite(socket, op);
if (success) {
op.onFailure.accept(new InterruptedByTimeoutException());
}
}, timeout, unit);
}
socket.writeOperation = op;
} finally {
socket.writeLock.unlock();
}
selector.wakeup();
startedWrites.increment();
currentWrites.increment();
return op;
}
/**
* java.nio.channels.InterruptedByTimeoutException#InterruptedByTimeoutException()
*/
public void test_empty() {
InterruptedByTimeoutException e = new InterruptedByTimeoutException();
assertTrue(e instanceof IOException);
assertNull(e.getMessage());
assertNull(e.getLocalizedMessage());
assertNull(e.getCause());
}
ReadOperation startRead(
RegisteredSocket socket,
ByteBufferSet buffer,
long timeout,
TimeUnit unit,
LongConsumer onSuccess,
Consumer<Throwable> onFailure)
throws ReadPendingException {
checkTerminated();
Util.assertTrue(buffer.hasRemaining());
waitForSocketRegistration(socket);
ReadOperation op;
socket.readLock.lock();
try {
if (socket.readOperation != null) {
throw new ReadPendingException();
}
op = new ReadOperation(buffer, onSuccess, onFailure);
/*
* we do not try to outsmart the TLS state machine and register for both IO operations for each new socket
* operation
*/
socket.pendingOps.set(SelectionKey.OP_WRITE | SelectionKey.OP_READ);
if (timeout != 0) {
op.timeoutFuture =
timeoutExecutor.schedule(
() -> {
boolean success = doCancelRead(socket, op);
if (success) {
op.onFailure.accept(new InterruptedByTimeoutException());
}
},
timeout,
unit);
}
socket.readOperation = op;
} finally {
socket.readLock.unlock();
}
selector.wakeup();
startedReads.increment();
currentReads.increment();
return op;
}
WriteOperation startWrite(
RegisteredSocket socket,
ByteBufferSet buffer,
long timeout,
TimeUnit unit,
LongConsumer onSuccess,
Consumer<Throwable> onFailure)
throws WritePendingException {
checkTerminated();
Util.assertTrue(buffer.hasRemaining());
waitForSocketRegistration(socket);
WriteOperation op;
socket.writeLock.lock();
try {
if (socket.writeOperation != null) {
throw new WritePendingException();
}
op = new WriteOperation(buffer, onSuccess, onFailure);
/*
* we do not try to outsmart the TLS state machine and register for both IO operations for each new socket
* operation
*/
socket.pendingOps.set(SelectionKey.OP_WRITE | SelectionKey.OP_READ);
if (timeout != 0) {
op.timeoutFuture =
timeoutExecutor.schedule(
() -> {
boolean success = doCancelWrite(socket, op);
if (success) {
op.onFailure.accept(new InterruptedByTimeoutException());
}
},
timeout,
unit);
}
socket.writeOperation = op;
} finally {
socket.writeLock.unlock();
}
selector.wakeup();
startedWrites.increment();
currentWrites.increment();
return op;
}