728x90
반응형
void interrupt() : Thread의 interrupted 상태를 false에서 true로 변경
boolean interrupted() : Thread interrupted 상태를 반환
static boolean interrupted():Thread interrupted 상태를 반환후,false로변경


import javax.swing.JOptionPane;
public class Ex13_9 {
public static void main(String[] args) {
ThreadEx9_1 th1 = new ThreadEx9_1();
th1.start();
String input = JOptionPane.showInputDialog("아무값이나 입력하세요");
System.out.println("입력하신 값은 " + input + "입니다.");
th1.interrupt(); //interrupt()를 호출하면 interrupted()상태가 true가 된다.
System.out.println("inInterrupted():" + th1.isInterrupted()); //true
}
}
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("카운트가 종료되었습니다.");
}
}

import javax.swing.JOptionPane;
public class Test101 {
public static void main(String[] args) throws Exception {
ThreadEx9_2 th1 = new ThreadEx9_2();
System.out.println("폭탄이 동작을 시작했습니다.");
System.out.println("10초 안에 암호를 입력하세요.");
th1.start();
String input;
while(true) {
input = JOptionPane.showInputDialog("암호는 ?");
if(input == null) {
continue;
}
if(input.equals("Apple")) {
break;
}
}
System.out.println("입력하신 값은 " + input + "입니다.");
th1.interrupt();
System.out.println("isInterrupted():"+ th1.isInterrupted());
}
}
class ThreadEx9_2 extends Thread {
public void run() {
int i = 10;
while(i!=0 && !isInterrupted()) {
System.out.println(i--);
//for(long x=0;x<2500000000L;x++); // 시간 지연
//for(long x=0;x<2500000000L;x++){} // 시간 지연일때는 {} 이렇게 바로 닫아준다.
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
System.out.println("카운트가 종료되었습니다.");
}
}728x90
반응형
'Language > Java' 카테고리의 다른 글
| TCP UDP (0) | 2021.04.05 |
|---|---|
| InetAddress 클래스 (0) | 2021.04.05 |
| 교착상태 (0) | 2021.04.02 |
| JOptionPane (0) | 2021.04.02 |
| Thread의 I/O blocking (0) | 2021.04.02 |
| Daemon Thread (0) | 2021.04.02 |