알고 싶습니다. 예를 들어 r.wait()가 작동합니까? 이 코드 :notify/wait()를 사용하여 특정 (그룹) 스레드를 깨우거나 일시 중단 할 수 있습니까?
public class Buffer1<T> {
private T content;
private boolean empty;
private Object r = new Object();
private Object w = new Object();
public Buffer1() {
empty = true; }
public Buffer1(T content) {
this.content = content;
empty = false; }
public T take() throws InterruptedException {
synchronized (r) {
while (empty) {
r.wait();
}
synchronized (w) {
empty = true;
w.notify();
return content;
}
}
}
public void put(T o) throws InterruptedException {
synchronized(w) {
while (!empty) {
w.wait();
}
synchronized (r) {
empty = false;
r.notify();
content = o;
}
방법 r.wait 않습니다(), r.notify w.wait는(),(), w.notify은() 일? 동기화 된 (r)/동기화 된 (w)과 함께 어떻게 작동합니까?
글쎄, 당신의 인생은'notifyAll()'을 사용하면 더 간단 해 질 것이다 ... 특정 스레드 그룹을 깨우기로되어 있지 않다. 그것은 깨어납니다. 이것이'wait()'가 항상 루프 안에서 발생하는 이유입니다. 잠에서 깨어 나면 스레드 자체가 잠에서 깨어 나면 좋은 시간인지 확인하고 그렇지 않으면 다시 wait()로 돌아갑니다. 그러나 wake와 notify는 동시 프로그래밍을위한 저수준 구조이기 때문에 동시 모듈을 더 간단하게 만드는 Executor Framework를 사용해야합니다. – scottb
또는'java.util.concurrent' 패키지의 세마포어. – EJP