본문 바로가기
프로그래밍 언어/JAVA(자바) 응용

72.wait(), notify() 메서드를 활용한 동기화 프로그래밍

by lroot 2022. 6. 7.
728x90
반응형

wait() / notify()에서 메서드를 활용한 동기화 프로그래밍

- 리소스가 어떤 조건에서 더 이상 유효하지 않은 경우 리소스를 기다리기 위해 Thread가 wait()상태가 된다.

- wait() 상태가 된 Thread은 notify()가 호출 될 때까지 기다린다.

- 유효한 자원이 생기면 notify()가 호출되고 wait()하고 있는 Thread 중 무작위로 하나의 Thread를 재시작 하도록 한다.

- notifyAll()이 호출되는 경우 wait() 하고 있는 모든 Thread가 재시작 된다.

- 이 경우 유효한 리소스만큼의 Thread만이 수행될 수 있고 자원을 갖지 못한 Thread의 경우는 다시 wait() 상태로 만든다.

- 자바에서는 notify() 메서드의 사용을 권장한다.

- 도서관에서 책을 빌리는 예제

- LibraryMain.java

class FastLibrary{

public ArrayList<String> shelf = new ArrayList();

public FastLibrary() {
shelf.add("해리포터1");
shelf.add("해리포터2");
shelf.add("해리포터3");

}

public synchronized String lendBook() throws InterruptedException{

Thread t = Thread.currentThread();
while(shelf.size() == 0) {
System.out.println(t.getName() + "waiting start");
wait();
System.out.println(t.getName() + "waiting end");
}

if(shelf.size() > 0) {
String book = shelf.remove(0);
System.out.println(t.getName() + " : "+book+" lend ");
return book;
}
else return null;
}

public synchronized void returnBook(String book) {

Thread t = Thread.currentThread();

shelf.add(book);
notify();
System.out.println(t.getName() + " : "+book+" return ");
}
}

class Student extends Thread{

public Student(String name) {
super(name);
}
public void run() {

try {

String title = LibraryMain.library.lendBook();
if(title == null) {
System.out.println(getName()+" : 빌리지 못함");
return;
}

sleep(5000);
LibraryMain.library.returnBook(title);

} catch (InterruptedException e) {

System.out.println(e);
}


}
}

public class LibraryMain {

public static FastLibrary library = new FastLibrary();

public static void main(String[] args) {

Student std1 = new Student("학생1 ");
Student std2 = new Student("학생2 ");
Student std3 = new Student("학생3 ");
Student std4 = new Student("학생4 ");
Student std5 = new Student("학생5 ");
Student std6 = new Student("학생6 ");

std1.start();
std2.start();
std3.start();
std4.start();
std5.start();
std6.start();
}

}

 

댓글