• Post Reply Bookmark Topic Watch Topic
  • New Topic

Superfluous notifyAll() Good Programming Practice?  RSS feed

 
Anthony Watson
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your code synchronizes on an object (myObject) and, at the time you are writing the code, there is currently no thread that calls myObject.wait(), is it still good programming practice to put notifyAll() at the end of all your synchronized blocks in anticipation of future code calling myObject.wait() ?
 
Henry Wong
author
Sheriff
Posts: 23282
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMO,it is not a good idea. You wait() because you need a condition to exist. You notify() when the condition exist... Whatever this condition is, in the future, I am willing to bet that it is not because you are about to exit a synchronized block.

Henry
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
synchronizing and waiting are different things. you dont wait because you cant access a synchronized block of code. You wait for some logical reason, like the bus has not arrived yet or its not 5pm yet. synchronizing and waiting are totally different concepts. Though to wait you will need to use synchronization.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!