public void produce() {
synchronized(queue) {
while(queue.isFull()) {
queue.wait();
}
//produce
queue.enqueue(someObj);
queue.notifyAll();
}
}
public void consume() {
synchronized(queue) {
while(queue.isEmpty()) {
queue.wait();
}
//consume
someObj = queue.dequeue();
//do something with the object
queue.notifyAll();
}
}
The code is pretty simple but even there are things where people get confused easily. First of all both the producer and consumer should synchronize on same object. Beginners tend to forget that. Also notice the use of while loop, when required conditions haven't been met threads are made to wait. A while loop is needed because we are using notifyAll here.notifyAll wakes up all the threads waiting on an object but only one thread will be able to obtain the lock. So in this case only one thread is allowed to proceed and others go back to wait. Another question arising in the context is the placement of notifyAll. Does it have to be the last statement inside synchronize block? The answer is no. It doesn't matter where you keep the notifyAll till it is inside synchronize block. The lock will only be released when synchronize block is escaped.
Recommendation
Never try to do produce consumer problem this way in production code. Always try to use the concurrent collections or facilities provided by the Java Language. For example, you could have used BlockingQueue rather than hand coding the program above.
No comments:
Post a Comment