1. sleep() - static메서드
- 현재 쓰레드를 지정된 시간동안 멈추게 한다.
static void sleep(long millis) // 천분의 일초 단위
static void sleep(long millis, int nanos) // 천분의 일초 + 나노초
- 예외처리를 해야 한다.(InterruptedException이 발생하면 깨어남)
try{
Thread.sleep(1, 500000); // 쓰레드를 0.0015초 동안 멈추게 한다.
} catch (InterruptedException e) {}
void delay(long millise) {
try{
Thread.sleep(millis);
} catch(InterruptedException e) {}
}
delay(1500);
- 특정 쓰레드를 지정해서 멈추게 하는 것은 불가능하다.
try {
th1.sleep(2000); // 에러는 아님
} catch(InterruptedException e) {}
try {
Thread.sleep(2000);
} catch(InterruptedException e) {}
2. 예제
public class Ex_05 {
public static void main(String[] args) {
ThreadEx8_1 th1 = new ThreadEx8_1();
ThreadEx8_2 th2 = new ThreadEx8_2();
th1.start();
th2.start();
try{
th1.sleep(2000); // main쓰레드를 2초동안 잠재운다.
} catch(InterruptedException e) {} // 오해의 여지가 있으니 th1을 Thread.sleep()로 쓴다.
delay(2000);
System.out.print("<<main 종료>>");
}
static void delay(long millis) {
try{
Thread.sleep(millis);
} catch(InterruptedException e) {}
}
}
class ThreadEx8_1 extends Thread {
public void run() {
for (int i = 0; i < 300; i++) {
System.out.print("-");
}
System.out.print("<<th1종료>>");
}
}
class ThreadEx8_2 extends Thread {
public void run() {
for (int i = 0; i < 300; i++) {
System.out.print("|");
}
System.out.print("<<th2종료>>");
}
}
3. interrupt()
- 대기상태(WAITING)인 쓰레드를 실행대기 상태(RUNNABLE)로 만든다.
void interrupt() // 쓰레드의 interrupted상태를 false에서 true로 변경.
boolean isInterrupted() // 쓰레드의 interrupted상태를 반환.
static boolean interrupted() // 현재 쓰레드의 interrupted상태를 알려주고, false로 초기화
ThreadEx13_2 th1 = new ThreadEx13_2();
th1.start();
...
th1.interrupt(); // interrupt()를 호출하면, interrupted상태가 true가 된다.
...
System.out.println("isInterrupted():"+ th1.isInterrupted()); // true
4. 예제
public class Ex_06 {
public static void main(String[] args) {
ThreadEx9_1 th1 = new ThreadEx9_1();
th1.start();
System.out.println("isInterrupted():"+ th1.isInterrupted()); // false
String input = JOptionPane.showInputDialog("아무 값이나 입력하세요.");
System.out.println("입력하신 값은 "+ input + "입니다.");
th1.interrupt(); // interrupt()를 호출하면, interrupted상태가 true가 된다.
System.out.println("isInterrupted():"+ th1.isInterrupted()); // true
System.out.println("isInterrupted():"+ th1.isInterrupted()); // true
// main쓰레드가 interrupt되었는지 확인
System.out.println("interrupted():"+ Thread.interrupted()); // false
}
}
class ThreadEx9_1 extends Thread{
public void run() {
int i = 10;
while (i != 0 && !isInterrupted()) {
System.out.println(i--);
for(long x = 0; x<2500000000L;x++); // 시간지연
}
System.out.println("isInterrupted():"+ isInterrupted()); // false
System.out.println("isInterrupted():"+ this.isInterrupted()); // false
// isInterrupted()와 달리 interrupted()는 interrupted라는 상태변수를 false로 초기화 한다.
System.out.println("interrupted():"+ Thread.interrupted()); // true
System.out.println("interrupted():"+ Thread.interrupted()); // false
System.out.println("interrupted():"+ Thread.interrupted()); // false
System.out.println("카운트가 종료되었습니다.");
}
}
'Java' 카테고리의 다른 글
133. join(), yield() - 패스트캠퍼스 백엔드 부트캠프 3기 (0) | 2025.01.07 |
---|---|
132. suspend(), resume(), stop() - 패스트캠퍼스 백엔드 부트캠프 3기 (0) | 2025.01.07 |
130. 데몬 쓰레드, 쓰레드의 상태 - 패스트캠퍼스 백엔드 부트캠프 3기 (0) | 2025.01.06 |
129. 쓰레드의 우선순위, 쓰레드 그룹 - 패스트캠퍼스 백엔드 부트캠프 3기 (2) | 2025.01.06 |
128. 싱글쓰레드와 멀티쓰레드, 쓰레드의 I_O블락킹 - 패스트캠퍼스 백엔드 부트캠프 3기 (0) | 2025.01.06 |