package ex19;
public class Th02 {
static String product = null; // 공유 자원인 상품 변수
public static void main(String[] args) {
// 첫 번째 스레드 (상품 공급 스레드)
Thread supp = new Thread(() -> {
try {
Thread.sleep(10000); // 10초 동안 대기
product = "바나나깡"; // 상품 입고
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
supp.start();
// 두 번째 스레드 (상품 확인 스레드)
Thread lis = new Thread(() -> {
while (true) {
try {
Thread.sleep(500); // 0.5초마다 대기
if (product != null) { // 상품이 입고되었는지 확인
System.out.println("상품이 입고되었습니다. : " + product); // 메시지 출력
break; // 루프 종료
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
lis.start();
}
}

Share article