This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Even though Wait is called , it is still running the Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Even though Wait is called , it is still running the Thread" Watch "Even though Wait is called , it is still running the Thread" New topic
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
    
    1

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
    
    1

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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Even though Wait is called , it is still running the Thread
 
Similar Threads
dan exam doubt 20
Thread question - uCertify 6.0
stop does release the lock
simple wait and notify is not working
delete method code