[JAVA-9895] Split concurrency-basic-2 module
This commit is contained in:
@@ -12,7 +12,6 @@ This module contains articles about basic Java concurrency
|
||||
- [Guide to AtomicMarkableReference](https://www.baeldung.com/java-atomicmarkablereference)
|
||||
- [Why are Local Variables Thread-Safe in Java](https://www.baeldung.com/java-local-variables-thread-safe)
|
||||
- [How to Stop Execution After a Certain Time in Java](https://www.baeldung.com/java-stop-execution-after-certain-time)
|
||||
- [How to Handle InterruptedException in Java](https://www.baeldung.com/java-interrupted-exception)
|
||||
- [How to Get the Number of Threads in a Java Process](https://www.baeldung.com/java-get-number-of-threads)
|
||||
- [Set the Name of a Thread in Java](https://www.baeldung.com/java-set-thread-name)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-concurrency-basic)
|
||||
- [[<-- Prev]](../core-java-concurrency-basic)[[Next -->]](../core-java-concurrency-basic-3)
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.baeldung.concurrent.interrupt;
|
||||
|
||||
public class CustomInterruptedException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
CustomInterruptedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -7,12 +7,6 @@
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user