Language/Java

교착상태

원2 2021. 4. 2. 15:06
728x90
반응형

sleep() 처럼 쓰레드를 멈추게 만드는 

suspend() : 쓰레드를 멈추게 만듬

resume() : 다시 실행대기 상태로 만듬

stop() : 쓰레드를 멈추게 만듬

suspend, stop : 는 교착상태를 만들기 때문에 사용을 권장하지 않는다.

교착 상태를 일으킨다. 

public class Ex13_10 {

	public static void main(String[] args) {
		RunImplEx10 r = new RunImplEx10();
		Thread th1 = new Thread(r, "*");
		Thread th2 = new Thread(r, "**");
		Thread th3 = new Thread(r, "***");
		th1.start();
		th2.start();
		th3.start();
		
		try {
			Thread.sleep(2000);
			th1.suspend();  	// Thread th1을 잠시 중단시킨다.
			Thread.sleep(2000);
			th2.suspend();
			Thread.sleep(3000);
			th1.resume();    	// Thread th1을 다시 동작하도록 시킨다.
			Thread.sleep(3000);
			th1.stop();       	// Thread th1을 강제종료 시킨다.
			th2.stop();
			Thread.sleep(2000);
			th3.stop();
		} catch (InterruptedException e) {}
	} // main

}
class RunImplEx10 implements Runnable {
	public void run () {
		while (true) {
			System.out.println(Thread.currentThread().getName());
			try {
				Thread.sleep(1000);
				
			} catch (InterruptedException e) {}
		}
	} // run
}
728x90
반응형

'Language > Java' 카테고리의 다른 글

TCP Socket프로그래밍  (0) 2021.04.05
TCP UDP  (0) 2021.04.05
InetAddress 클래스  (0) 2021.04.05
interrupt  (0) 2021.04.02
JOptionPane  (0) 2021.04.02
Thread의 I/O blocking  (0) 2021.04.02