Java : InterruptedException with Examples
InterruptedException (Java SE 21 & JDK 21) with Examples.
You will find code examples on most InterruptedException methods.
Summary
try (final var executor = Executors.newSingleThreadExecutor()) {
System.out.println("-- submit --");
final var future = executor.submit(() -> {
try {
System.out.println("sleep ...");
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
System.out.println("InterruptedException!");
}
});
TimeUnit.SECONDS.sleep(1);
System.out.println("-- interrupt --");
future.cancel(true);
}
// Result
// ↓
//-- submit --
//sleep ...
//-- interrupt --
//InterruptedException!
Constructors
InterruptedException ()
final var e = new InterruptedException();
System.out.println(e); // java.lang.InterruptedException
InterruptedException (String s)
final var e = new InterruptedException("abcd");
System.out.println(e); // java.lang.InterruptedException: abcd
Methods declared in Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
Please see the link below.
Related posts
- API Examples
- BlockingQueue
- Callable
- CancellationException
- ConcurrentHashMap.KeySetView
- ConcurrentLinkedDeque
- ConcurrentLinkedQueue
- ConcurrentMap
- ConcurrentModificationException
- ConcurrentSkipListSet
- Condition
- CopyOnWriteArrayList
- CopyOnWriteArraySet
- CountDownLatch
- CyclicBarrier
- Exchanger
- Executor
- ExecutorService
- Executors
- Future
- Future.State
- FutureTask
- Lock
- Runnable
- Semaphore
- Thread
- ThreadGroup
- ThreadLocal
- TimeUnit