my dog learned polymorphism*
The moose likes Threads and Synchronization and the fly likes Main method finishes but other threads still running Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Main method finishes but other threads still running" Watch "Main method finishes but other threads still running" New topic
Author

Main method finishes but other threads still running

azuki ooh
Greenhorn

Joined: Nov 07, 2008
Posts: 20
I was hinted by lecturer that the Cook is not a thread - it is a shared resource since any of the savages can wake up the cook. The cook also does not need to know how many servings are in the pot, since its responsibility is just to cook.

Hence, I have modified my program as such:








When I run this, the debugger tells me that my main thread finished and there are still some other threads waiting. I don't understand the flow of process. Can someone help me understand the flow of process here and why there are still threads waiting?
>
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

azuki ooh wrote:When I run this, the debugger tells me that my main thread finished and there are still some other threads waiting. I don't understand the flow of process. Can someone help me understand the flow of process here and why there are still threads waiting?


Walk through the execution. What does the main method do? What does it do after it starts the Savage threads?


Steve
azuki ooh
Greenhorn

Joined: Nov 07, 2008
Posts: 20
I have been trying to walk through the execution, but I might have understand it in a wrong direction.
Here's my understanding:

In main thread:
Create a cook object
Create a numOfServing to hold an integer
Create 10 Savage object
Start running the Savage
Savage object will run 2 times.
In each run, it check the value in numOfServing.
If it's 0, then it call the putServingsInPot() in cook, then it goes into wait.
so processing is now at putServingsInPot(). And the end of this method, it calls the notifyAll to wake up the thread that's waiting.
so Savage thread continue, it checks and find that numOfServing is now >0, so it process the part after the while loop.

This sounds correct to me. But the output is wrong. Obviously my understanding is wrong. But what is wrong? Can someone tell me please?

I think I need a join() in the main to make sure the all other thread ends before the main thread. But the program just got "hang", the threads simple wait and nothing happen. That is why I comment out the join part in the main().
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

azuki ooh wrote:When I run this, the debugger tells me that my main thread finished and there are still some other threads waiting. I don't understand the flow of process. Can someone help me understand the flow of process here and why there are still threads waiting?

This quote is from the first post in this thread. Regarding this issue, you say:
In main thread:
1) Create a cook object
2) Create a numOfServing to hold an integer
3) Create 10 Savage object
4) Start running the Savage
5) Savage object will run 2 times.
6) In each run, it check the value in numOfServing.
7) If it's 0, then it call the putServingsInPot() in cook, then it goes into wait.
8) so processing is now at putServingsInPot(). And the end of this method, it calls the notifyAll to wake up the thread that's waiting.
9) so Savage thread continue, it checks and find that numOfServing is now >0, so it process the part after the while loop.

Numbering mine.

You are correct up until to step 4. At step 5 and on, what thread is doing that action? What else does the main thread do after starting the Savage?

I think I need a join() in the main to make sure the all other thread ends before the main thread

That is correct. If you want the main thread to wait until the Savage threads are complete, you need to join them.

But the program just got "hang", the threads simple wait and nothing happen. That is why I comment out the join part in the main().

But with this, you are now talking about a different problem. This is the same problem I warned you about in the other thread. This is the danger of taking your question to multiple threads. Your issue becomes fragmented. Rather than answer the question about the hang here, I will refer you back to that other thread. Re-read my post and then ask further questions about it there.
azuki ooh
Greenhorn

Joined: Nov 07, 2008
Posts: 20
Why does my method getAServing() gets interrupted even though I already have synchronized keyword?







My output:
Cook has put 1 missionaries into pot
there are 1 in pot now.
Savage 3 getting a serving to eat.
There are no more missionaries in the pot!
There are no more missionaries in the pot!
There are no more missionaries in the pot!
There are no more missionaries in the pot!
There are no more missionaries in the pot!
There are no more missionaries in the pot!
There are no more missionaries in the pot!
There are no more missionaries in the pot!
There are no more missionaries in the pot!
Savage 3 finished eating.
Cook has put 1 missionaries into pot
there are 1 in pot now.
Savage 1 getting a serving to eat.
Cook has put 1 missionaries into pot
there are 1 in pot now.
Savage 7 getting a serving to eat.
Cook has put 1 missionaries into pot
there are 1 in pot now.
Savage 9 getting a serving to eat.
Cook has put 1 missionaries into pot
there are 1 in pot now.
Savage 5 getting a serving to eat.
Savage 7 finished eating.
Savage 9 finished eating.
Savage 1 finished eating.
Savage 5 finished eating.
Cook has put 2 missionaries into pot
there are 2 in pot now.
Savage 2 getting a serving to eat.
Savage 2 finished eating.
Cook has put 3 missionaries into pot
there are 3 in pot now.
Savage 4 getting a serving to eat.
Savage 4 finished eating.
Cook has put 4 missionaries into pot
there are 4 in pot now.
Savage 10 getting a serving to eat.
Savage 10 finished eating.
Cook has put 5 missionaries into pot
there are 5 in pot now.
Savage 8 getting a serving to eat.
Cook has put 5 missionaries into pot
there are 5 in pot now.
Savage 6 getting a serving to eat.
Savage 8 finished eating.
Savage 6 finished eating.
There are 4 missionaries left in the pot.



I thought when a Savage thread run, it calls the getAServing. when it finds that numOfServings[0]==0, it should complete
numOfServings[0]+=c.putServingsInPot(); goes on the next line until the method is complete with numOfServings[0]--; All these is in a synchronized method.
But my output seems like before the assignment to numOfServings[0], the next thread run n check that numOfServings[0]==0 and print "There are no more missionaries in the pot!"

But I have already used synchronized.

The correct output supposed to look like this:
Cook has put 5 missionaries into pot
Savage 0 getting a serving to eat
Savage 3 getting a serving to eat
Savage 5 getting a serving to eat
Savage 1 getting a serving to eat
Savage 9 getting a serving to eat
There are no more missionaries in the pot!
Cook has put 2 missionaries into pot
Savage 5 finished eating
Savage 3 finished eating
Savage 0 finished eating
Savage 1 finished eating
Savage 9 finished eating
Savage 7 getting a serving to eat
Savage 8 getting a serving to eat
There are no more missionaries in the pot!
Cook has put 5 missionaries into pot
Savage 8 finished eating
Savage 7 finished eating
Savage 6 getting a serving to eat
Savage 4 getting a serving to eat
Savage 2 getting a serving to eat
Savage 4 finished eating
Savage 6 finished eating
Savage 2 finished eating
There are 2 missionaries left in the pot







>
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

I told you why in the other thread. Please read the linked post.
azuki ooh
Greenhorn

Joined: Nov 07, 2008
Posts: 20
Manage to solve this. Posting this to share with those who are reading this forum to learn something.















>
 
GeeCON Prague 2014
 
subject: Main method finishes but other threads still running