
쓰레드의 상태를 알기 위해서는 getState 메소드를 사용하면 된다.
public Thread.State getState()
만약 Thread 클래스의 currentThread로 현재 쓰레드를 얻은 다음 get
Thread.currentThread().getState();
import static java.lang.Thread.*;
public class Main {
private static Thread myThread;
private static Thread stateThread;
public static void main(String[] args) {
stateThread = new Thread(() -> {
while(true) {
State state = myThread.getState();
System.out.println(state);
if (state == State.NEW)
myThread.start();
if (state == State.WAITING)
myThread.interrupt();
if (state == State.TERMINATED) {
break;
}
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
myThread = new TestThread(stateThread);
stateThread.start();
}
static class TestThread extends Thread {
private Thread target;
public TestThread(Thread target) {
this.target = target;
}
@Override
public void run() {
for(int i = 0; i <= 2000000000; i++);
for(int i = 0; i <= 2000000000; i++);
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
target.join();
} catch (InterruptedException e) {
for(int i = 0; i <= 2000000000; i++);
for(int i = 0; i <= 2000000000; i++);
}
}
}
}

public class Main {
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(new CustomThread(1, 4000));
Thread thread2 = new Thread(new CustomThread(2, 4000));
Thread thread3 = new Thread(new CustomThread(3, 2000));
Thread thread4 = new Thread(new CustomThread(4, 3000));
Thread thread5 = new Thread(new CustomThread(5, 10000));
thread5.start();
thread3.start();
thread1.start();
thread5.join();
thread3.join();
thread1.join();
thread2.start();
thread4.start();
}
public static class CustomThread implements Runnable {
private int number;
private int millis;
public CustomThread(int number, int millis) {
this.number = number;
this.millis = millis;
}
@Override
public void run() {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(number);
}
}
}

interrupt 호출 시점 또는 이후 쓰레드가 일시정지 상태(= 플래그가 true)가 되면 InterruptedException을 발생시켜 쓰레드를 종료시킨다.
InterruptedException은 다음과 같은 상황에서 발생한다.
- 대기 상태(WAITING 또는 TIMED_WAITING)에서 어떤 쓰레드가 interrupt를 호출해 플래그가 true가 되는 경우
- 어떤 쓰레드가 interrupt를 호출해 쓰레드의 플래그 true인 상태에서 작업 스레드에 제어권이 넘어오고 sleep 등의 메서드가 호출되며 대기상태로 변할 때
InterruptedException이 발생하면
쓰레드 인터럽트 플래그가 자동으로 false가 된다.
isInterrupted : 쓰레드 인터럽트 플래그를 변화시키지 않고 확인만 한다.interrupted : 인터럽트 상태를 반환한 뒤 false로 만든다.public class Main {
public static void main(String[] args) throws InterruptedException {
TestThread thread = new TestThread();
thread.start();
Thread.sleep(3000);
thread.interrupt();
System.out.println("isInterrupted() : "+ thread.isInterrupted());
}
static class TestThread extends Thread{
@Override
public void run() {
int count = 10;
while (count != 0){
System.out.println(count--);
for (long i = 0 ; i < 2500000000L; i++);
}
System.out.println("종료");
}
}
}
출력 결과를 보면

인터럽트가 호출됐어도 계속 쓰레드가 진행되는 것을 볼 수 있다. 이는 플래그는 interrupt를 호출하며 true로 변하였지만 쓰레드가 대기상태에 들어가지 않아서 그렇다.
public class Main {
public static void main(String[] args) throws InterruptedException {
TestThread thread = new TestThread();
thread.start();
Thread.sleep(400);
thread.interrupt();
System.out.println("isInterrupted() : "+ thread.isInterrupted());
}
static class TestThread extends Thread{
@Override
public void run() {
int count = 10;
try{
while (count != 0){
System.out.println(count--);
Thread.sleep(100);
}
}catch (InterruptedException e){
System.out.println("종료");
}
}
}
}
이번에는 대기를 for문을 통해서 하지 않고 sleep을 이용해서 했다. 따라서 쓰레드가 대기 상태로 들어가고 아래와 같이 작업을 모두 끝내기 전에 InterruptedException이 발생해 숫자를 모두 출력하지 못한다.

public class Main {
public static void main(String[] args) throws InterruptedException {
ThreadA threadA = new ThreadA();
ThreadB threadB = new ThreadB();
threadA.start();
threadB.start();
Thread.sleep(2000);
threadA.work = false;
System.out.println("--------------------양보 시작 ----------------------");
Thread.sleep(2000);
System.out.println("--------------------양보 끝 ----------------------");
threadA.work = true;
Thread.sleep(2000);
threadA.stop = true;
threadB.stop = true;
}
public static class ThreadA extends Thread {
public boolean stop = false;
public boolean work = true;
public void run() {
while (!stop) {
if (work) {
System.out.println("ThreadA 작업");
for (long i = 0; i < 1500000000L; i++);
} else {
Thread.yield();
}
}
System.out.println("ThreadA 종료");
}
}
public static class ThreadB extends Thread {
public boolean stop = false;
public boolean work = true;
public void run() {
while (!stop) {
if (work) {
System.out.println("ThreadB 작업");
for (long i = 0; i < 1500000000L; i++);
} else {
Thread.yield();
}
}
System.out.println("ThreadB 종료");
}
}
}