Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Even though Wait is called , it is still running the Thread  RSS feed

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

Even though i am calling wait , it is still running the Thread .




Any ideas . please advise .
 
Stephan van Hulst
Saloon Keeper
Posts: 6980
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any body please explain .
 
Stephan van Hulst
Saloon Keeper
Posts: 6980
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!