1 분 소요

보조스레드 종료시키기

  • join()메소드

서브 스레드의 종료를 기다리기에도 한계가 있다(보조 스레드가 영원히 안 끝날 수 도 있다)

if (th1.isAlive())
			System.out.println("th1이 아직 작업중입니다");
		try {
			th1.join(2000);// 조인을 해서 기다린다 보조스레드를 기다린다
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		System.out.println("=========Main Exit============");
sub1: NEW
 sub1: NEW
 sub1: NEW
 sub1: NEW
 sub1: RUNNABLE
 th1이 아직 작업중입니다
Main: RUNNABLE
 sub1[14] : 1
sub1[14] : 2
sub1[14] : 3
sub1[14] : 4
sub1[14] : 5
sub1[14] : 6
sub1[14] : 7
sub1[14] : 8

.
.
.
=========Main Exit============
sub1[14] : 92
sub1[14] : 93

 

 

  • interrupt() 메소드

보조스레드에게 그만하면 안되겠니~? 요청


		if (th1.isAlive())
			System.out.println("th1이 아직 작업중입니다");
		try {
			th1.join(2000);// 조인을 해서 기다린다 보조스레드를 기다린다
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		th1.interrupt();// interrupt (요청) 그만하면 안되니?-> 메인 스레드가 끝날때

		System.out.println("=========Main Exit============");
	}

	private static void print() {

		Thread th = Thread.currentThread();

		for (int i = 0; i < 1000000000; i++) {
//			try {
//				Thread.sleep(20);
//			} catch (InterruptedException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}

			if (th.isInterrupted()) {
				System.out.println("============ th Interrupted=============");
				return;
			}

			if (th.getName().equals("Main"))
				System.out.printf("<%s[%d] : %d\n", th.getName(), th.getId(), i + 1);
			else
				System.out.printf("%s[%d] : %d\n", th.getName(), th.getId(), i + 1);
		}
	}
}

sub1[14] : 112093
sub1[14] : 112094
sub1[14] : 112095
sub1[14] : 112096
sub1[14] : 112097
sub1[14] : 112098
sub1[14] : 112099
sub1[14] : 112100
sub1[14] : 112101

=========Main Exit============
============ th Interrupted=============
Main: TERMINATED

  • sleep값을 줄 떄의 interrupt

		if (th1.isAlive())
			System.out.println("th1이 아직 작업중입니다");
		try {
			th1.join(2000);// 메인에는 하위 스레드를 기다릴 수 는 있다 :max값을 지정한다
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		th1.interrupt();// interrupt (요청) 그만하면 안되니?-> 메인 스레드가 끝날때

		System.out.println("=========Main Exit============");
	}

	private static void print() {

		Thread th = Thread.currentThread();

		for (int i = 0; i < 1000000000; i++) {
			try {
				Thread.sleep(200000000);// 조건검사를 못하는 경우
										// interupted를 통해 깨워짐
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				System.out.println("누가 날깨운거?");
				return;
			}

			if (th.isInterrupted()) {
				System.out.println("============ th Interrupted=============");
				return;
			}

			if (th.getName().equals("Main"))
				System.out.printf("<%s[%d] : %d\n", th.getName(), th.getId(), i + 1);
			else
				System.out.printf("%s[%d] : %d\n", th.getName(), th.getId(), i + 1);
		}
	}
}

sub1: NEW
 sub1: NEW
 sub1: NEW
 sub1: NEW
 sub1: RUNNABLE
 th1이 아직 작업중입니다
Main: TIMED_WAITING
 =========Main Exit============
누가 날깨운거?
Main: TERMINATED

카테고리:

업데이트:

댓글남기기