• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loop in thread doesn't complete  RSS feed

 
Branislav Devrnja
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am studying threads in Java. I came across behavior of loops in thread that I don't understand. It seems to me that for loop that encounters "wait"
doesn't complete, after wait was notified.
Here is the code.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a casual look at the code, it seems that you are waiting in a loop and notifying only once.
First notification does get to the waiting thread and thats why you get in the output ms.wait() but after that there is no one to notify the waiting thread. So, the wait does not finish.
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ms is a local variable in each thread class. Waiting/notifying on a local variable is worthless.

Create a variable in your main() and pass it to both threads. Use that variable to wait/notify.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Edward Harned:
ms is a local variable in each thread class.

Nope, that is a reference to a static member of class Threadmethods.
Originally posted by Edward Harned:

Create a variable in your main() and pass it to both threads. Use that variable to wait/notify.

How is it different from what is being done now?
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a casual look at the code


Yes its static. Missed it in the casual.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nitesh Kant:
On a casual look at the code, it seems that you are waiting in a loop and notifying only once.
First notification does get to the waiting thread and thats why you get in the output ms.wait() but after that there is no one to notify the waiting thread. So, the wait does not finish.


Yep, that's it. The variable "count" gets to be greater then 10,000 pretty fast, since it's a running sum of each value of the loop variable; I'm not sure that's what was intended here. But in any case, the first wait() returns and prints the message, then the loop calls wait() again (since "count" is still more than 10,000) and that second wait() never returns.
 
sarvesh meens
Ranch Hand
Posts: 43
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thread comes out of wait the first time due to spurious wake-up.
This is exactly the reason why wait() should always conform to the std idiom:



Can anyone tell me reason for spurious wake-up?
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sarvesh meens:
The thread comes out of wait the first time due to spurious wake-up.

Oh man, thats interesting. So, how did you come to that conclusion?
 
Ugender Rekulampally
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Branislav,
The output you showed will also vary on how those two threads run first/fast.

In your output, if you wanted to see this line

gets executed, then you could use a 'break' after your System.out.println("ms.wait()"). the reason is count is already 10000 and variable 'i' still not reached to 10000 so loop is going to run even after wait() is done and it is going to wait() again. in this case, no one notify() ing again to get the wait() out till variable 'i' reaches 10000 (+1).

Lets assume Notifier thread runs and executes below code



before Job1 Thread gets to this below code



Then you will see different output. the reason is your Notifier gets the lock on ms and notifies before Job1 gets the lock.

So in this situation, you dont even see "ms.wait()" print statement also because there is no one notifying this thread at all. and it will wait() forever.

What exactly you wanted to see in your output?


Thanks,
Ugender
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[sarvesh meens]: The thread comes out of wait the first time due to spurious wake-up.

[Nitesh]: Oh man, thats interesting. So, how did you come to that conclusion?


I really don't think there's any reason to think this is the case. Spurious wakeup is something that can happen, according to the spec (at least since JLS 3rd edition, anyway - see 17.8.1) but it's generally rare, and on some platforms may never occur. It is something that should be allowed for as a possibility though, and that's one reason why wait() should alwas be performed in a loop that checks a condition, as sarvash showed. I think this is just a miscommunication - sarvash said that spurious wakeup does happen when he (or she?) meant that it can happen. Of course I may be mistaken, and I hope sarvash will let us know if that's the case.
[ December 16, 2007: Message edited by: Jim Yingst ]
 
sarvesh meens
Ranch Hand
Posts: 43
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Nitesh]: Oh man, thats interesting. So, how did you come to that conclusion?


Got carried away by the spurious wake-up angle.

The output observed could be obtained with out any spurious wake-up.

Sorry for the misguidance !

Jim,thanks for correcting.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!