Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

chapter06 : synchronized 본문

JAVA/chapter21_thread

chapter06 : synchronized

GAWON 2023. 5. 30. 18:34
package org.joonzis.ex;

class MyThread3 implements Runnable{
	/*
	 * Thread.wait()
	 * Object.notify()
	 * 
	 *   - 모니터링 락 설정 메소드
	 *   - synchronized 블럭 내에서 사용해야 함
	 */
	
	static int tot;
	@Override
	public void run() {
		synchronized (this) {
			for(int i=0; i<5; i++) {
				System.out.println((i+1) + "더하기");
				tot += (i+1);
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			notify(); // wait() 하고 있는 객체를 깨움
		}
	}
}
public class Ex06_synchronized {
	public static void main(String[] args) {
		Thread t2 = new Thread(new MyThread3());
		t2.start();
		
		// 만약 main 쓰레드가 아래의 블록이 위의 Thread 보다 먼저 실행되었다면 wait 하게 되면서
		// 모니터링 락을 놓고 대기
		synchronized (t2) {
			try {
				// t2.wait() 메소드를 호출
				// 메인 쓰레드는 정지
				// 메소드에서 다섯 번 값을 더한 뒤 notify()를 호출하게 되면 wait()에서 깨어남
				System.out.println("다른 쓰레드가 완료될 때까지 기다립니다.");
				t2.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("총 합 : "  + MyThread3.tot);
		}
		
	}
}

'JAVA > chapter21_thread' 카테고리의 다른 글

Test . thread  (0) 2023.05.30
chapter05 : synchronized  (0) 2023.05.30
chapter04 : ObjectShare  (0) 2023.05.30
chapter03 : mulitThread  (0) 2023.05.30
chapter02 : multiThread  (0) 2023.05.30