• Post Reply Bookmark Topic Watch Topic
  • New Topic

simple wait and notify is not working  RSS feed

 
amit sharma
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



This Program hang after printing " Hi I am Ant" and does not finish
please anyone tell Whats Wrong in this i am able to figure it out
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried running the code. It works perfectly fine. I get both the outputs and the program exits successfully.
 
amit sharma
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i run above code Linux(Fedora12) , jdk 1.6. Program Just Hang after Printing "Hi i am Ant" and Does not Finish

On Windows Xp , JDK 1.6, above code works as expected. giving Output

Hi i am Ant
Hi i am main

why it is happening? i don't think these things depend on the operating system

 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hold on. Let me try out in my Ubuntu system.
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Works fine in Ubuntu!
 
amit sharma
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks John, it is working fine on my friend's ubuntu!
anyway i will make my project in windows
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try with NotifyAll()?
 
amit sharma
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried it with notifyAll(), but the same thing happen again. i will post here if i find the solution, if anyone who has similar problem with solution then let me know.
 
Stephan van Hulst
Saloon Keeper
Posts: 7719
142
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have created indeterministic behaviour by creating a race condition.

Your main thread and the Ant thread are both racing to reach their synchronized blocks the first. In your case, your Ant thread reaches its synchronized block first, gets the lock. So the main thread has to wait for the Ant thread to finish. After the Ant thread prints "Hi i am Ant" it calls this.notify (which should be notifyAll by the way), but there are no other threads waiting on a! This is because the main thread hasn't reached the wait block yet, because it's outside waiting for a lock on the Ant object.
Finally, the Ant thread is done, and the main thread can now go into the synchronized block. Here it stumbles on the wait() call, and waits indefinitely because there's nothing to wake it up.

This works on other computers, because by chance on these computers/OSes, the main thread is the first one to reach the synchronized block. Like I said, this is indeterministic.

To solve your problem, switch these two lines of code in the main method:
a.start();
synchronized (a) {
 
amit sharma
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan yes it works.
I should read properly before making some serious program using threads
 
Stephan van Hulst
Saloon Keeper
Posts: 7719
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah multi-threaded programs can be a real pain. I like to think I know my way around them, but they remain difficult, and bite me in the ass time and again.

Practice lots.
 
Angel Taveras
Ranch Hand
Posts: 84
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make it deterministic you have to change the code to the one below as Stephan suggested



Regards,

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!