• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Problem with wait() / notify()

 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having a problem with my wait / notify code. I understand pretty much how it all works, but the problem is that notify() is being called before wait() and I'm not sure the best way to fix it.

Rather, than post my entire code, I've created this little application which demonstrates the problem.




The associated output is:

running...
wait...
Do something...
doing it...
...done it
notify...
Do something again...
doing it...
...done it
notify...
wait...



I can see why the problem happens, but is there an elegant way to solve it?

Cheers,
James

PS - Why I'm here, can anyone suggest a more elegant solution than "Thread.sleep(1000);" ?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't you need two loops in two separate threads? At the moment, other than the first call to doSomethingAndRepeat in the main method, you've got the calls to wait and notify in the same thread. Which means the thread is waiting for itself.

This probably isn't what you wanted, but here's a similar example where two threads take it in turns.


 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. I decided to go for a less elegant polling solution in the end. Cheers, James.
 
You can't expect to wield supreme executive power just because
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic