Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about loop in Thread run method!!  RSS feed

 
Kai Liu
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I found some question and couldn't figure it out~
wonder if someone can shed some light on this ?

I had some java code as following, it rans fine....
but when I tried to make it run for only 10 times,
I modified the loop in Waiter's run method like this



the program will freeze when count is about 5 or 6.....
does anybody have some clue ?

Thanks in advance,

Kai



 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The consumer - the Waiter - will go through the loop 10 times, but it only displays 5 things.

count(0) -> No Food, so it waits with no output.
count(1) -> Consumes food and tells us so.
count(2) -> No Food, so it waits...
etc...

Then, when the consumer is done with its 10 loops (5 printlns) then Cook is sitting there with a filled food tray and nothing to do with it (he never gets notified the work is complete) so he freezes there waiting to be notified. You have to tell him he is done.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!