This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Hello every 1 in the code given below why the threads that have been blocked once do not get into runnable move when called notifyAll(). Where i m wrong. I would appreciate if any one could help me out of this.
The problem is you have two different threads that are two different objects. So if you were to step through it: 1. object aa gets created named first 2. object bb gets created named second 3. object aa starts, prints out it's info, tells everyone that is waiting on aa that aa is about to be released, and then makes aa wait. 4. object bb starts, prints out it's info, tells everyone that is waiting on bb that bb is about to be released, and then makes bb wait. Calling notify or notifyAll will restart all threads that have called wait() for the object to which the notifyAll method belongs to. So calling notifyAll for thread bb only notifies bb, not aa. [This message has been edited by bill bozeman (edited December 06, 2000).]
Joined: Sep 29, 2000
hi Bill, Thanks for your reply, but please tell me one thing that as u told that notifyall will notify only bb object and not aa, then why it is not printing other bb in the run method. why it is printing just once and then the system hangs on. One more thing that now if i want to notify the thread of aa then how it can b done. Please explain me how can i get the desied result of this program. thanks
Joined: Jun 30, 2000
You can't notify a specific thread, you can only notify all threads that are waiting, but remember you are only notifing threads that is an object of the call to notify. So when the aa thread calls notify, it has no effect on bb. Why your systems hangs, that is because after you print out the info, you tell it to wait() so you moved aa into the waiting state. Since aa is waiting, it can't notify itself that it can go again. That is basically what you have when you have wait and notify in the same method. Method starts, prints, notify, but it has nothing to notify because no aa methods are waiting, and then waits, but hangs becuase there are no aa methods to notify it to start again. What you want to do is have wait and notify in two different methods. First method gets called, does what you want, then waits, this releases that lock and allows the other method to be called and in that method you have your notify. Don't put them in run() just have run call the methods. I am not an expert on threads, but that is how I have seen them. And since you can't tell which thread in the waiting state will start, it seems like common practice you have flags set up so that you can turn them on and off and then you have a while(flag) wait(); Play around with the code and see what happens. Bill