
Thread::isJoined 메서드는 주어진 스레드가 종료되었는지 여부를 반환합니다.
- 스레드가 종료되면 true를 반환합니다.
- 스레드가 종료되지 않았으면 false를 반환합니다.
Thread::isJoined을 사용하는 목적은 주어진 스레드의 상태를 확인하고, 스레드가 종료되었는지 여부를 확인하는 것입니다.
Thread::isJoined을 사용하는 방법은 다음과 같습니다.
1. 스레드가 종료되기 전에 isJoined을 호출하여 false를 반환하는지 확인합니다.
2. 스레드가 종료된 후 isJoined을 호출하여 true를 반환하는지 확인합니다.
3. 스레드의 상태를 확인하기 위해 반복적으로 isJoined을 호출할 수 있습니다.
예를 들어, 다음 코드는 스레드가 종료되기 전에 isJoined을 호출하여 false를 반환하는지 확인합니다.
#hostingforum.kr
java
public class Main {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
try {
Thread.sleep(1000); // 스레드가 종료되기 전에 sleep
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
thread.start();
System.out.println(thread.isAlive()); // true
System.out.println(thread.isInterrupted()); // false
System.out.println(thread.isAlive()); // true
System.out.println(thread.isInterrupted()); // false
thread.join(); // 스레드가 종료되기 전에 join
System.out.println(thread.isAlive()); // false
System.out.println(thread.isInterrupted()); // false
}
}
이 예제에서 스레드가 종료되기 전에 isAlive()를 호출하여 true를 반환하는지 확인합니다. 스레드가 종료된 후 isAlive()를 호출하여 false를 반환하는지 확인합니다.
2025-04-26 04:42