Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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

 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

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




Any ideas . please advise .
 
Saloon Keeper
Posts: 13486
304
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any body please explain .
 
Stephan van Hulst
Saloon Keeper
Posts: 13486
304
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic