From cbb5548525f8abc3e053c0d26ef9a79502d98156 Mon Sep 17 00:00:00 2001 From: MeenaGawande <45625809+MeenaGawande@users.noreply.github.com> Date: Thu, 22 Apr 2021 07:24:36 +0530 Subject: [PATCH] [BAEL-4296] Handling InterruptedException in Java (#10677) [BAEL-4296] Handling InterruptedException in Java Co-authored-by: MeenaGawande --- .../interrupt/CustomInterruptedException.java | 10 ++++ .../interrupt/InterruptExample.java | 47 +++++++++++++++++++ .../interrupt/InterruptExampleUnitTest.java | 33 +++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 core-java-modules/core-java-concurrency-basic-2/src/main/java/com/baeldung/concurrent/interrupt/CustomInterruptedException.java create mode 100644 core-java-modules/core-java-concurrency-basic-2/src/main/java/com/baeldung/concurrent/interrupt/InterruptExample.java create mode 100644 core-java-modules/core-java-concurrency-basic-2/src/test/java/com/baeldung/concurrent/interrupt/InterruptExampleUnitTest.java diff --git a/core-java-modules/core-java-concurrency-basic-2/src/main/java/com/baeldung/concurrent/interrupt/CustomInterruptedException.java b/core-java-modules/core-java-concurrency-basic-2/src/main/java/com/baeldung/concurrent/interrupt/CustomInterruptedException.java new file mode 100644 index 0000000000..a4189df31d --- /dev/null +++ b/core-java-modules/core-java-concurrency-basic-2/src/main/java/com/baeldung/concurrent/interrupt/CustomInterruptedException.java @@ -0,0 +1,10 @@ +package com.baeldung.concurrent.interrupt; + +public class CustomInterruptedException extends Exception { + + private static final long serialVersionUID = 1L; + + CustomInterruptedException(String message) { + super(message); + } +} diff --git a/core-java-modules/core-java-concurrency-basic-2/src/main/java/com/baeldung/concurrent/interrupt/InterruptExample.java b/core-java-modules/core-java-concurrency-basic-2/src/main/java/com/baeldung/concurrent/interrupt/InterruptExample.java new file mode 100644 index 0000000000..7d81769854 --- /dev/null +++ b/core-java-modules/core-java-concurrency-basic-2/src/main/java/com/baeldung/concurrent/interrupt/InterruptExample.java @@ -0,0 +1,47 @@ +package com.baeldung.concurrent.interrupt; + +public class InterruptExample extends Thread { + + public static void propagateException() throws InterruptedException { + Thread.sleep(1000); + Thread.currentThread().interrupt(); + if (Thread.interrupted()) { + throw new InterruptedException(); + } + } + + public static Boolean restoreTheState() { + InterruptExample thread1 = new InterruptExample(); + thread1.start(); + thread1.interrupt(); + return thread1.isInterrupted(); + } + + public void run() { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + + public static void throwCustomException() throws Exception { + + Thread.sleep(1000); + Thread.currentThread().interrupt(); + if (Thread.interrupted()) { + throw new CustomInterruptedException("This thread was interrupted"); + } + } + + public static Boolean handleWithCustomException() throws CustomInterruptedException{ + try { + Thread.sleep(1000); + Thread.currentThread().interrupt(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new CustomInterruptedException("This thread was interrupted..."); + } + return Thread.currentThread().isInterrupted(); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-concurrency-basic-2/src/test/java/com/baeldung/concurrent/interrupt/InterruptExampleUnitTest.java b/core-java-modules/core-java-concurrency-basic-2/src/test/java/com/baeldung/concurrent/interrupt/InterruptExampleUnitTest.java new file mode 100644 index 0000000000..1ea3f9aa9b --- /dev/null +++ b/core-java-modules/core-java-concurrency-basic-2/src/test/java/com/baeldung/concurrent/interrupt/InterruptExampleUnitTest.java @@ -0,0 +1,33 @@ +package com.baeldung.concurrent.interrupt; + +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +public class InterruptExampleUnitTest { + + @Test + public void whenPropagateException_thenThrowsInterruptedException() { + assertThrows(InterruptedException.class, () -> InterruptExample.propagateException()); + } + + @Test + public void whenRestoreTheState_thenReturnsTrue() { + assertTrue(InterruptExample.restoreTheState()); + } + + @Test + public void whenThrowCustomException_thenContainsExpectedMessage() { + Exception exception = assertThrows(CustomInterruptedException.class, () -> InterruptExample.throwCustomException()); + String expectedMessage = "This thread was interrupted"; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage)); + } + + @Test + public void whenHandleWithCustomException_thenReturnsTrue() throws CustomInterruptedException{ + assertTrue(InterruptExample.handleWithCustomException()); + } +}