B
Buck Turgidson
I am trying to get up to speed on threads, using Sun's tutorial. I think I
got most everything, but what is confusing me is simply how the put() method
(producer) ever breaks out of the while look and runs, because I don't see
how the available flag is ever "true" for him. It seems like it will always
be false, since the consumer exits get() leaving the flag set to false.
Can someone clear up my confusion?
public class CubbyHole {
private int contents;
private boolean available = false;
public synchronized int get() {
while (available == false) {
try {
wait();
} catch (InterruptedException e) { }
}
available = false;
notifyAll();
return contents;
}
public synchronized void put(int value) {
while (available == true) {
try {
wait();
} catch (InterruptedException e) { }
}
contents = value;
available = true;
notifyAll();
}
}
got most everything, but what is confusing me is simply how the put() method
(producer) ever breaks out of the while look and runs, because I don't see
how the available flag is ever "true" for him. It seems like it will always
be false, since the consumer exits get() leaving the flag set to false.
Can someone clear up my confusion?
public class CubbyHole {
private int contents;
private boolean available = false;
public synchronized int get() {
while (available == false) {
try {
wait();
} catch (InterruptedException e) { }
}
available = false;
notifyAll();
return contents;
}
public synchronized void put(int value) {
while (available == true) {
try {
wait();
} catch (InterruptedException e) { }
}
contents = value;
available = true;
notifyAll();
}
}