• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to do possible the thread which execute wait is running before another thread which call notify  RSS feed

 
barlet south
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a program and i am using multithreading just to get parallelism . The problem in my program is that the thread which execute notify is running before the thread which execute wait so " i am waiting forever ". The code is as below



The problem here is that the output is 5 times waiting for calculation so it never go and print the total is .... I thought to use while loop as it was used above but still is not working. Should someone tell me what i am doing wrong here?
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I shall duplicate this discussion in our threads forum.

Why are you using while (true)...? That is probably a mistake.
 
barlet south
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I shall duplicate this discussion in our threads forum.

That would be great. Could you please send me the link of this thread forum

Why are you using while (true)...? That is probably a mistake.


Even without using while it is not working as expected
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Extending Thread is usually a bad idea, and so it is here.



Since a Reader is a Thread, you're calling the start() method of Thread. Which calls the run() method of Thread, which you overrode to do nothing but call c.wait(). Note that you didn't use the Thread constructor which accepts a Runnable object so the Thread (a.k.a. Reader) doesn't have a Runnable object and so it doesn't call the run() method of Calculator.

Now if Reader had implemented Runnable, things would have been very different.

Although you seem to think that your "wait" code is going to wait until all five of the calculation threads have finished. Or something like that, I can't really tell. I get the feeling that you don't really know what to expect either, and you're just throwing in random stuff like "while (true)" to see if it fixes what you think is a problem.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
barlet south wrote: I am writing a program and i am using multithreading just to get parallelism . The problem in my program is that the thread which execute notify is running before the thread which execute wait so " i am waiting forever ".


I don't actually see any threads that executes notify(). You create five threads that all wait().

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!