• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wait and notify

 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which part of the code Have you been confused about?
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21427
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


why is the output here is
even
even.
.
.
.multiple times...why there isnt one notified?
 
Filipe Pinheiro
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
superb.....the main thing you did here is creating a new thread inside the run loop.....thanks a lot....this certainly helped
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic