[JAVA-9895] Split concurrency-basic-2 module
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
## Core Java Concurrency Basic
|
||||
|
||||
This module contains articles about basic Java concurrency.
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [How to Handle InterruptedException in Java](https://www.baeldung.com/java-interrupted-exception)
|
||||
- [[<-- Prev]](../core-java-concurrency-basic-2)
|
||||
27
core-java-modules/core-java-concurrency-basic-3/pom.xml
Normal file
27
core-java-modules/core-java-concurrency-basic-3/pom.xml
Normal file
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-concurrency-basic-3</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-concurrency-basic-3</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-concurrency-basic-3</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.concurrent.interrupt;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
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