• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Java Threads: wait+notify

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If I just create this object with an initial value of 1000 in the main function and start it, it prints "Timer ticked" every second.

But it doesn't work to wake waiting threads with this code:
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your synchronization is definitely broken. Before calling wait() and notify() on some object you must obtain the lock for this object. In Timer you're synchronizing on this and then calling this.notifyAll() (everything's fine), but in Timertest you're synchronizing on this and calling timer.wait() - and I guess you see an exception.
 
Marshal
Posts: 65388
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a "beginning" topic. Moving to our threads forum.
 
Vega Zimur
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, it says "interrupted" every time the for loop executes again.
but how do I get the lock of the timer object? -> what do I have to add to the code?

If I change the method of TestTimer like this, I don't get an error, but the Thread waits forever:
 
Garik Ustinov
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't get an error, but the Thread waits forever.



Yup, it does indeed. The problem is that run() method in Timer is synchronized, which means it holds the lock all the time and never releases it, so TimerTest never has a chance to get it, so it just waits. Instead of synchronizing the whole method you have to narrow the synchronized block, so the Timer thread would release the lock from time to time.
What you need is something like this (and remove synchronized from method declaration)
 
Vega Zimur
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, if I do it like this, it works

 
Garik Ustinov
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well done about putting sleep outside synchronized - I've put it wrong.
PS. there's no need to declare run() as synchronized in TestTimer. You don't really want to use more locks than you need, otherwise one day you may end up in a deadlock
 
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!