본문 바로가기

Java/Java

(JAVA) 쓰레드의 실행제어 - suspend(), resume(), stop(), yield()

suspend(), resume(), stop()

suspend() 쓰레드를 정지 
resume() suspend()에 의해 정지된 쓰레드를 실행대기 상태로
stop() 호출되는 즉시 쓰레드를 종료

쓰레드의 실행을 제어할 수 있는 가장 손 쉬운 방법이지만 

deadlock을 일으키기 쉽다.

 

현재는 deprecated 되어있다.

 


yield() - 다른 쓰레드에게 양보한다.

스케쥴러에게 1초의 실행시간을 할당받은 쓰레드가 있다고 가정하자

0.5초에 yield()를 호출하면 나머지 0.5초는 포기하고 실행대기 상태에 들어가게 된다.

 

이때 yield()와 interrupt()를 적절히 사용하면 프로그램의 응답성을 높히고 효율적인 실행이 가능하게 만들 수 있다.

 

예시

public class ThreadYield {
    public static void main(String[] args) {
        /*thread 인스턴스 생성*/
        ThreadYield_1 th1 = new ThreadYield_1("th1");
        ThreadYield_1 th2 = new ThreadYield_1("th2");
        ThreadYield_1 th3 = new ThreadYield_1("th3");
        /*thread 실행*/
        th1.start();
        th2.start();
        th3.start();
        System.out.println("thread 시작!!!");
        try {
            Thread.sleep(2000);
            th1.suspend();
            System.out.println(Thread.currentThread().getName()+" suspend!!");


            Thread.sleep(2000);

            th2.suspend();
            System.out.println(Thread.currentThread().getName()+" suspend!!");


            Thread.sleep(3000);

            th1.resume();
            System.out.println(Thread.currentThread().getName()+" resume!!");

            Thread.sleep(3000);

            th1.stop();

            Thread.sleep(2000);
            th3.stop();
        } catch (InterruptedException e) {

        }
    }
}
class ThreadYield_1 implements Runnable{

    boolean suspended = false;
    boolean stopped = false;

    Thread th;

    public ThreadYield_1(String name) {
        th = new Thread(this, name);
    }

    @Override
    public void run() {
        String name = th.getName();
        while (!stopped) {
            if (!suspended) {
                System.out.println("현재 thread :: "+name);
                try {

                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println(name + "-interrupted");
                }
            }else {
                Thread.yield();
            }
        }
        System.out.println(name + "-stopped");
    }
    public void suspend() {
        suspended = true;
        th.interrupt();
        System.out.println(th.getName() + "-interrupt () by suspend()");
    }
    public void resume() {
        suspended = false;
    }
    public void stop () {
        stopped = true;
        th.interrupt();
        System.out.println(th.getName() + "-interrupt () by stop()");
    }
    public void start() {
        th.start();
    }
}

위의 코드는 크게 네 가지의 쓰레드가 존재한다.

  • main Thread
  • th1 Thread
  • th2 Thread
  • th3 Thread

suspend(), stop() 메서드를 살펴보면 suspend의 boolean 값을 true로 초기화하고 interrupt()를 발생시킨다.

resume() 메서드는 suspend boolean값을 다시 true로 초기화한다.

 

또한 suspend 값이 true라면 (잠시 실행을 멈춘 상태) yield()를 호출해서 다른 쓰레드에게 양보한다.

(의미없는 while문을 반복하지 않기 위해)

 

실행결과

현재 thread :: th1
현재 thread :: th2
현재 thread :: th3
thread 시작!!!
현재 thread :: th2
현재 thread :: th3
현재 thread :: th1
th1-interrupt () by suspend()
th1-interrupted
main suspend!!
현재 thread :: th2
현재 thread :: th3
현재 thread :: th2
현재 thread :: th3
th2-interrupt () by suspend()
main suspend!!
th2-interrupted
현재 thread :: th3
현재 thread :: th3
현재 thread :: th3
main resume!!
현재 thread :: th1
현재 thread :: th3
현재 thread :: th1
현재 thread :: th3
현재 thread :: th1
현재 thread :: th3
th1-interrupt () by stop()
th1-interrupted
th1-stopped
현재 thread :: th3
현재 thread :: th3
th3-interrupt () by stop()
th3-interrupted
th3-stopped

 


코드 참조

https://github.com/mike6321/PURE_JAVA/tree/master/Thread

 

mike6321/PURE_JAVA

Contribute to mike6321/PURE_JAVA development by creating an account on GitHub.

github.com