-
Java - Stop, InterruptJava 2018. 1. 31. 12:07반응형public class StopFlag extends Thread {boolean stop = false;@Overridepublic void run() {while(!stop) {System.out.println("Thread Running...");}System.out.println("종료");}}public class Main {public static void main(String[] args) throws InterruptedException {StopFlag stop = new StopFlag();stop.start();Thread.sleep(1000);stop.stop = true;}}< Interrupt >// InterruptedException을 발동 시키려면 Sleep이 존재해야 한다// interrupt()가 발생하면 catch의 InterruptedException의 데이터로 이동된다public class Inter extends Thread {@Overridepublic void run() {// try {// while(true) {// System.out.println("Thread Running...");// Thread.sleep(1); // InterruptedException을 발동 시키려면 Sleep이 존재해야 한다// }// } catch (InterruptedException e) {// System.out.println("종료");// }// try-catch문도 안하고, sleep도 안하는 법while(true) {System.out.println("Thread Running...");if(Thread.interrupted()) {break;}}System.out.println("종료");}}public class Main {public static void main(String[] args) throws InterruptedException {// Interrupt Exception 활용Inter inter = new Inter();inter.start();Thread.sleep(800);inter.interrupt();}}반응형
'Java' 카테고리의 다른 글
Java - Thread Pool, Thread Pool Blocking (0) 2018.01.31 Java - Deamon Thread, Thread Group (0) 2018.01.31 Java - Wait, Notify (0) 2018.01.31 Java - Thread (0) 2018.01.30 Java - 정규표현식 (0) 2018.01.26 댓글