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

Is There Such Thing As An Implicit Wait?  RSS feed

 
Anthony Watson
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you have code like this:


does this implicitly add the currently executing thread to the list of threads that should be informed when another thread calls myObject.notifyAll()?

I'm pretty sure that there is no implicit wait, but if not, shouldn't there be?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I suppose it depends what you mean by "informed". If another thread has the lock on myObject, this thread won't be able to enter the synchronized block at first. So this thread will block, and it will not execute until the other thread exits its synhronized block and this thread acquires the lock. Behind the scenes, the JVM is managing process a bit. So you could say that the JVM has a list of threads that are trying to acquire the lock on myObject, and when that lock becmoes available, the JVM will "inform" one of those blocking threads, allowing it to execute.

However - this has nothing at all to do with notifyAll(). So I suspect it's not what you meant to ask about. In order for notifyAll() to have any significance, the thread must have already synchronized on myObject (as shown in your code) and then called the wait() method (not shown). If you call wait, the thread will wait until notified. If you don't call wait, then notification is irrelevant.

No - to do that, you need to call myObject.wait(). Which can only be done from inside code that is synchronized on myObject, such as you have shown. So you're partway there. But unless you actually call
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!