Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Thread's wait-notify

Thread view: 
gk - 21 Nov 2005 12:48 GMT
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
Thomas Hawtin - 21 Nov 2005 13:21 GMT
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Robert Klemme - 21 Nov 2005 13:45 GMT
>> i dont understand Thread's wait-notify . how do i use it ? where to
>> use it ?
[quoted text clipped - 21 lines]
>
> 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
Knute Johnson - 21 Nov 2005 16:13 GMT
>>>i dont understand Thread's wait-notify . how do i use it ? where to
>>>use it ?
[quoted text clipped - 53 lines]
>
>     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 :-).

Signature

Knute Johnson
email s/nospam/knute/

Chris Uppal - 21 Nov 2005 13:45 GMT
> *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
Jeffrey Schwab - 21 Nov 2005 14:21 GMT
>>*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.

Is a new edition of that book available?  The most recent I can find is
from 1999.
Thomas Hawtin - 21 Nov 2005 14:52 GMT
>>> *The* book on the subject is Concurrent Programming in Java: Design
>>> Principles and Patterns by Doug Lea.
[quoted text clipped - 6 lines]
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Chris Uppal - 21 Nov 2005 14:59 GMT
> > > *The* book on the subject is Concurrent Programming in Java: Design
> > > Principles and Patterns by Doug Lea.
[...]
> 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
Roedy Green - 21 Nov 2005 17:19 GMT
On Mon, 21 Nov 2005 14:21:03 GMT, Jeffrey Schwab
<jeff@schwabcenter.com> wrote, quoted or indirectly quoted someone who
said :

>Is a new edition of that book available?  The most recent I can find is
>from 1999.
that's the most recent I know of, second edition. It is out of print.
For bookstore links see http://mindprod.com/jgloss/deadlock.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 21 Nov 2005 17:03 GMT
>can anybody help me to understand this[wait] ? please provide a simple
>example and explanation to understand this.

see http://mindprod.com/jgloss/wait.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.