Thomas Hawtin wrote:
> gk wrote:
>> i dont understand Thread's wait-notify . how do i use it ? where to
>> use it ?
>
> The are methods in Object, not Thread. If you try to use them on a
> Thread object you may not get the results you expect.
>
>> i have read sun java tutorial and also mindprod java tutorial....but
>> are not clear about this concept.
>>
>> can anybody help me to understand this ? please provide a simple
>> example and explanation to understand this.
>
> It is not simple.
>
> *The* book on the subject is Concurrent Programming in Java: Design
> Principles and Patterns by Doug Lea.
>
> http://www.amazon.co.uk/exec/obidos/ASIN/0201310090/
>
> 1.5 changes low-level details, but does not alter what most
> programmers do. I have just noticed there is an online supplement at:
>
> http://gee.cs.oswego.edu/dl/cpj/
>
> Tom Hawtin
Sample mini queue - this is typical usage of wait/notify
import java.util.LinkedList;
public class MiniQueue {
private final Object lock = new Object();
private final LinkedList storage = new LinkedList();
public void enqueue( Object o ) {
synchronized ( lock ) {
storage.addLast( o );
lock.notifyAll();
}
}
public Object dequeue() throws InterruptedException {
synchronized ( lock ) {
while ( storage.isEmpty() ) {
lock.wait();
}
return storage.removeFirst();
}
}
}
Kind regards
robert