Norbert Muench wrote:Looking at the code, I would say this will throw an IllegalMonitorStateException in line 14, because the wait call is not in a synchronized block.
Norbert Muench wrote:As far as I can see, there's no guarantee that the output is "Ready to print, Now Printing, ". It could also be "Now Printing, Ready to Print, ", correct?
Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Norbert Muench wrote:Same as you would call any other method.
The thread code still might get stuck in the call to wait() though, as there is no guarantee that the notifyAll() wouldn't be excuted before the wait().
Norbert Muench wrote:Same as you would call any other method.
The thread code still might get stuck in the call to wait() though, as there is no guarantee that the notifyAll() wouldn't be excuted before the wait().
Janki Shah wrote:Thank you Norbert for your explanation. So, in order to work with wait(), notify(), and notifyAll()... All these three methods have to acquire a lock on an object not on threads, Right? And wait() and notify() or notifyAll() they both have to get lock on the same object to work with each other? Please correct me if I am wrong.
Janki Shah wrote:But this conflicts with this " the thread must own the lock on the object that wait() is being invoked on " From K & B book chapter 9 question 7.
I am very confused in this logic who should own the lock on whom??? Can you please explain this?
Matheus Souza wrote:I have another question: when I use the syncronized keyword in a static member it means that all my template (static member - variables and methods) are lockeds?
Helen Ma wrote:By the way, will the following scenario be possible ?
Norbert Muench wrote:
Matheus Souza wrote:I have another question: when I use the syncronized keyword in a static member it means that all my template (static member - variables and methods) are lockeds?
No, it means that threads executing the synchronized method need to aquire a lock on the class object.
Matheus Souza wrote:So if I create new instace of MyClass, the synchronized methods would be locked already?
Paul Clapham wrote:
Matheus Souza wrote:So if I create new instace of MyClass, the synchronized methods would be locked already?
I'm not sure quite what this means, but let me answer one of the questions it might mean:
No, creating an object will not automatically cause any code to acquire any locks it didn't already have.
If it meant something else, please correct my guess.
Matheus Souza wrote:This is not what meant. I want to know if I lock an method syncronized, the whole set of static members would be locked too?
Norbert Muench wrote:
Matheus Souza wrote:This is not what meant. I want to know if I lock an method syncronized, the whole set of static members would be locked too?
I think you misunderstand how locking works. If one thread holds a lock to an object, it doesn't prevent other threads from modifying any of the objects member variables. It just prevents other threads from aquiring a lock to the same object. And threads will only aquire a lock when starting a synchronized block or synchronized method.
Take the following code as an example:
Now imagine you have two threads, thread 1 and thread 2. Thread 1 has just called MyClass.staticSyn1() and is in the middle of working through that method. So it holds a lock to the MyClass class object.
If thread 2 would start running now, it could modify the static member MyClass.si1. The fact that thread 1 holds the lock on the MyClass class object doesn't come into play here at all. Thread 2 could also call MyClass.static1(), because that class method isn't synchronized.
But what happens, if thread 2 tries to call MyClass.staticSyn1(), MyClass.staticSyn2() or syn1() on any object of type MyClass?
At the start of the synchronized block, the thread would try to aquire a lock on MyClass.class. That lock is already held by thread 1 however. So in this case thread 2 would be moved from the running state to the blocked state. Once thread 1 releases the lock on MyClass.class, thread 2 would aquire the locks and would be moved from blocked to runnable (and eventually to running).
Consider Paul's rocket mass heater. |