
join() 함수를 호출한 스레드가 먼저 종료되는 경우, join() 함수는 InterruptedException을 발생시키고 종료됩니다.
예를 들어, 다음과 같은 코드를 작성할 수 있습니다.
#hostingforum.kr
java
public class Main {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
try {
Thread.sleep(1000); // 1초간 작업을 수행합니다.
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // 인터럽트를 재설정합니다.
}
});
thread.start();
thread.join(); // join() 함수를 호출합니다.
}
}
이 경우, thread.join() 함수는 InterruptedException을 발생시키고 종료됩니다.
이러한 상황에서 join() 함수를 호출한 스레드가 먼저 종료되는 것을 처리하는 방법은 InterruptedException을 catch하여 처리하는 것입니다.
예를 들어, 다음과 같은 코드를 작성할 수 있습니다.
#hostingforum.kr
java
public class Main {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
try {
Thread.sleep(1000); // 1초간 작업을 수행합니다.
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // 인터럽트를 재설정합니다.
}
});
thread.start();
try {
thread.join(); // join() 함수를 호출합니다.
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // 인터럽트를 재설정합니다.
System.out.println("join() 함수가 InterruptedException을 발생시켰습니다.");
}
}
}
이 경우, join() 함수가 InterruptedException을 발생시키면 catch 블록에서 처리됩니다.
이러한 방법으로 join() 함수를 호출한 스레드가 먼저 종료되는 것을 처리할 수 있습니다.
2025-06-20 05:03