Ed's latest article: A Java Reactive Generator http://coopsoft.com/ar/ReactiveArticle.html
Originally posted by vikas chess:
Edward Harned, thanks for the link.
i know the basics of thread and synchronization. it would be highly appreciable if you can figure out the bug in above code.
Originally posted by Henry Wong:
Vikas,
I think it may be a good idea to start with the link provided by Edward. You should have an understanding of the basics of the wait/notify mechanism -- not to mention that the tutorial has plenty of examples.
Henry
Ed's latest article: A Java Reactive Generator http://coopsoft.com/ar/ReactiveArticle.html
Originally posted by vikas chess:
man, i already have good understanding of threads and synchronization related stuffs. i too have the theoritical knowledge of wait and notify/notifyAll stuffs.
now i want to implement wait-notify. but i stuck in the above code. and now i am granting help from experts. it would be great if you can do correction in the above code.
Originally posted by Henry Wong:
Basically, wait and notify is object based. Thread A is waiting on a different object than Thread B. So thread B notify is lost as there is no thread waiting for it.
Henry
[ June 28, 2007: Message edited by: Henry Wong ]
Originally posted by Anupam Sinha:
Hi Vikas
Though invoking wait() inside the while loop is a good idea. Though it may have given you the desired output there's still problems with the code. I would suggest that you follow the tutorial and try and answer that were asked in this thread.
Originally posted by Anupam Sinha:
There are still some problems with your program. What if the t2 runs first and invokes notifyAll much before t1 has started to wait on lock.
Originally posted by Anupam Sinha:
Infact in one of your attempt you solved this. Try seeing one of your above post. This would also give you an idea why wait should always be invoked inside of a while loop.
but now the question is why in the above code, loop is not required to enclose wait() as stated in the note below:
http://java.sun.com/docs/books/tutorial/essential/concurrency/guardmeth.html
Originally posted by Henry Wong:
First, you should always check to see if you should wait. In your example, what if the notified happened first. There is not enough state between the two threads, you should not use a notification blindly.
What if you have multiple waiting threads? It is possible for a notifier to send the notification, but before a thread wakes up, an already awoke thread goes and handles it (which unsets the flag)... so Second, you should always check a flag after you wake up from a wait() as it may have been already handled.
So how do you do both of thesse? Create a flag, and check it in a while loop -- exact what you have done.
Henry
[ June 29, 2007: Message edited by: Henry Wong ]
I've been selected to go to the moon! All thanks to this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|