File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wait and notify Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wait and notify" Watch "wait and notify" New topic
Author

wait and notify

Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
i am trying to learn wait and notify.....i am trying one thread to go through a for loop from 1 to 10....if a number is even..i want the thread to print the number and wait...and let another thread print "even" ...and notify the first thread....and then print 4 then even from another thread...and soon... that is 0 even 2 even 4 even 6 even 8 even 10 even

i have tried this...but am getting confused...can anyone help me
Filipe Pinheiro
Greenhorn

Joined: Oct 08, 2009
Posts: 9
Which part of the code Have you been confused about?
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
once the notify is called.....the statement after wait() runs...how do i call wait() and wait for notify again? if i use while(true) with notify it doesnt work....
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19067
    
  40

Ankur kothari wrote:once the notify is called.....the statement after wait() runs...


Not true. It depends on which thread gets the lock first. If the notification is sent before the waiting thread gets to the wait(), meaning no thread is waiting, the notification will be lost.


Ankur kothari wrote:how do i call wait() and wait for notify again? if i use while(true) with notify it doesnt work....


What do you mean by not work? Keep in mind that waiting for multiple notifications, imply that multiple notification will be sent too.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531


why is the output here is
even
even.
.
.
.multiple times...why there isnt one notified?
Filipe Pinheiro
Greenhorn

Joined: Oct 08, 2009
Posts: 9
When the notify() is called, the method run() within the object thread "MyThread" reaches the end. So, "MyThread" object is dead, for this reason you can't anymore "wait()" for its methods. You have to review your code logic.
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
but am calling it within while(true)....and even when the notify() is called the first time....the wait() is not called...i mean it doesnt resume the code after wait()
Filipe Pinheiro
Greenhorn

Joined: Oct 08, 2009
Posts: 9
Take a look at this peace of code, actually i haven't tried it yet, but i think it might help you.

Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
superb.....the main thing you did here is creating a new thread inside the run loop.....thanks a lot....this certainly helped
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait and notify