Thread's wait-notify

G

gk

hi,
i dont understand Thread's wait-notify . how do i use it ? where to use
it ?

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.

thank you
 
T

Thomas Hawtin

gk said:
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
 
R

Robert Klemme

Thomas said:
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.


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
 
C

Chris Uppal

Thomas said:
*The* book on the subject is Concurrent Programming in Java: Design
Principles and Patterns by Doug Lea.

Very much agreed. Including your emphasis on the word "the".

One of the very, very, few books of which I have bought more than one edition.

-- chris
 
J

Jeffrey Schwab

Chris said:
Thomas Hawtin wrote:




Very much agreed. Including your emphasis on the word "the".

One of the very, very, few books of which I have bought more than one edition.

Is a new edition of that book available? The most recent I can find is
from 1999.
 
T

Thomas Hawtin

Jeffrey said:
Is a new edition of that book available? The most recent I can find is
from 1999.

It's the sort of thing that doesn't change frequently. There's a link to
a quick list of updates below. He says "Someday, I hope there will be a
third edition of this book." The biggest difference is that the
utilities described in the book are now part of java.util.concurrent.

http://gee.cs.oswego.edu/dl/cpj/updates.html

Tom Hawtin
 
C

Chris Uppal

Jeffrey said:
[...]
Is a new edition of that book available? The most recent I can find is
from 1999.

As far as I know the latest edition is the second, which was first published in
'99.

I don't know whether to expect another edition. On one hand the new
concurrency stuff in JDK5/JLS3 could use some quality coverage (and nobody is
better placed to provide that than Doug Lea). On the other hand the previous
edition gives you all the tools you need to understand the new stuff[*], so why
bother ? My guess is that it depends on the state of Mr Lea's pension fund ;-)

([*] the new concurrency library anyway, perhaps not the new language spec.)

-- chris
 
K

Knute Johnson

Robert said:
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

Along with Robert's excellent example, you use wait/notify when you want
one thread or many threads to stop execution until another thread sends
a notify message. Threads can take turns executing or as in his example
block until there is data to act on. The execution flow can be quite
complex with timeouts and interrupts. The Doug Lea book that Thomas
mentioned is very good but is probably not for beginners. Wasn't for me
when I first read it anyway :).
 
Joined
Sep 14, 2011
Messages
2
Reaction score
0
Step-wise explanation for wait and notify for the Queue Example?

Dear Robert,

If u could plz explain the Queue example, it would help me understand the difficult concept of wait(),notify() for Threads. Specifically, I don not understand the meaning of notify() and notifyAll().

Suppose one thread has entered the monitor, then how wait(),notify and notifyAll works? Plz explain step-wise....Thanks before hand....

Regards,
Aatish Pandya

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top