| Author |
Even though Wait is called , it is still running the Thread
|
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2234
|
|
Hi ,
Even though i am calling wait , it is still running the Thread .
Any ideas . please advise .
|
Save India From Corruption - Anna Hazare.
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3047
|
|
There are a few problems with your code. One, the thread that will display the message has been started before you do any synchronization, and there is a good chance it will print the message before you even get a chance to lock anything. You have made a race condition.
Secondly, you are synchronizing the main thread on the created object. The actual thread you want to wait is not waiting, because it never called wait (only System.out.println()).
Take a look at this example, see if you understand how it works:
|
 |
Javin Paul
Ranch Hand
Joined: Oct 15, 2010
Posts: 276
|
|
Yes , here the thread which is waiting is Main Thread instead of thread which is printing , to wait the printing thread put wait condition on printing thread instead on main thread.
|
http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
|
 |
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2234
|
|
Yes , here the thread which is waiting is Main Thread instead of thread which is printing , to wait the printing thread put wait condition on printing thread instead on main thread.
Thanks for the reply , but can you please tell me , how can i put a wait condition on the Printing Thread depending upon on the condition inside the Thread .
any sample piece of code on this please .
|
 |
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2234
|
|
|
Any body please explain .
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3047
|
|
Have your printing thread check a variable, and if it's not in the correct state, have it wait. This variable is set by your main thread.
Check the code I posted, it's all in there.
|
 |
 |
|
|
subject: Even though Wait is called , it is still running the Thread
|
|
|