Java : ClosedByInterruptException with Examples
ClosedByInterruptException (Java SE 24 & JDK 24) in Java with Examples.
You will find code samples for most of the ClosedByInterruptException methods.
Summary
Checked exception received by a thread when another thread interrupts it while it is blocked in an I/O operation upon a channel. Before this exception is thrown the channel will have been closed and the interrupt status of the previously-blocked thread will have been set.
try (final var executor = Executors.newSingleThreadExecutor()) {
final var future = executor.submit(() -> {
try (final var channel = ServerSocketChannel.open()) {
System.out.println("bind ...");
channel.bind(new InetSocketAddress("127.0.0.1", 8000));
System.out.println("bind OK!");
System.out.println("accept ...");
channel.accept();
System.out.println("accept OK!");
} catch (AsynchronousCloseException e) {
System.out.println(e);
} catch (IOException e) {
System.out.println("IOException!");
}
});
TimeUnit.SECONDS.sleep(2);
System.out.println("-- future.cancel --");
future.cancel(true);
}
// Result
// ↓
//bind ...
//bind OK!
//accept ...
//-- future.cancel --
//java.nio.channels.ClosedByInterruptException
Constructors
ClosedByInterruptException ()
Constructs an instance of this class.
final var e = new ClosedByInterruptException();
System.out.println(e); // java.nio.channels.ClosedByInterruptException
Methods declared in Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
Please see the link below.