Ch13-28~29. join(), yield()
0. 목차
Chapter13. 쓰레드
Ch13 - 28. join()과 yield()
Ch13 - 29. join()과 yield() 예제
Ch13 - 28 join()과 yield()
▶ join()이란?
▷ 지정 된 시간 동안 특정 쓰레드가 작업하는 것을 기다림
- 예를 들어, A가 입력이 되어야 B가 실행할 수 있으면, B는 join()으로 기다리게 함
▶ join() 관련 메서드
▷ void join()
- 시간 지정X : 작업이 모두 끝날 때 까지 기다림
▷ void join(long millis)
- 시간 지정O : millis(1/1000초) 동안 기다림
▷ void join(long millis, int nanos)
- 시간 지정O : millis(103)초 + nanos(109)초 동안 기다림
▶ join() 사용 방법
▷ 예외 처리 해야 함 : try-catch문
- sleep() 처럼
▷ InterruptedException 발생 시, 작업 재개
public static void main(String[] args) {
ThreadEx19 th1 = new ThreadEx19();
ThreadEx19 th2 = new ThreadEx19();
th1.start();
th2.start();
startTime = System.currentTimeMillis();
try {
th1.join(); // main 쓰레드가 th1 작업 완료할 때까지 기다림
th2.join(); // main 쓰레드가 th2 작업 완료할 때까지 기다림
} catch(InterruptedException e) {
System.out.println("소요시간 : " + (System.currentTimeMillis() - ThreadEx19.startTime));
}
}
▶ yield()란?
▷ 남은 시간을 다음 쓰레드에게 양보
▷ 자신(현재 쓰레드)은 실행 대기 함
▷ yield()도 static 메서드 : 자기 자신한테만 사용 가능
▷ yield()와 interrupt()를 적절히 사용하면 응답성과 효율 상승
Ch13 - 29. join()과 yield() 예제
▶ join() 예제1
▷ 시스템 실행 시간 구하기
▷ 실행 시작 시간 -기다렸다가→
실행 완료 시간 뺄셈
class Ex13_11 {
static long startTime = 0;
public static void main(String args[]) {
ThreadEx11_1 th1 = new ThreadEx11_1();
ThreadEx11_2 th2 = new ThreadEx11_2();
th1.start();
th2.start();
startTime = System.currentTimeMillis();
try {
th1.join(); // main 쓰레드가 th1의 작업이 끝날 때까지 기다림
th2.join(); // main 쓰레드가 th2의 작업이 끝날 때까지 기다림
} catch(InterruptedException e) {}
System.out.print("소요시간:" + (System.currentTimeMillis() - Ex13_11.startTime));
} // main
}
class ThreadEx11_1 extends Thread {
public void run() {
for(int i=0; i < 300; i++) {
System.out.print(new String("-"));
}
}
}
class ThreadEx11_2 extends Thread {
public void run() {
for(int i=0; i < 300; i++) {
System.out.print(new String("|"));
}
} // run()
}
// console
// 실행 시작 시간(startTime) - 현재 시간 = 9/1000초
// 실행 시작 시간 -기다렸다가(join())→ 실행 완료 시간 뺄셈
---------------------------------------------------------------------------
----------------------------------------------------------------|----||||||
|||--|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||-----
---------------------------------------------------------------------------
----------------------------------------------------------|||||------------|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||-----소요시간:9
// console
// join() 주석처리
// 실행 시작 시간 -기다리는 시간이 없음→ 실행 시작 하자마자 소요시간 거의 바로 찍어버림
-소요시간:0--------------------------------------------------------------------
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||