| Author |
Why won't the following Deadlock? Exam 2 q.16
|
Ai Ayumi
Greenhorn
Joined: May 26, 2012
Posts: 6
|
|
From K&B Practice Exam 2, q16:
Answer: there is only one lock, so no deadlock can occur.
Aren't synchronize(Stubborn.class) and static synchronized void push() locked seperately?
If only one lock, Does that mean if t1 is using synchronize(Stubborn.class), t2 won't be able to access static synchronized void push()?
Also, the output prints 5 4 3 2 1 (by t1), then 0 (by t2). Why is it not alternatingly by t1, t2?
Can someone help clarify?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Ai Ayumi wrote:
From K&B Practice Exam 2, q16:
Answer: there is only one lock, so no deadlock can occur.
Ai Ayumi wrote:
Aren't synchronize(Stubborn.class) and static synchronized void push() locked seperately?
Synchronized static methods use the instance of Class class (that represents the class which the method is declared) as its lock. So, a thread that calls a synchronized static method of the Stubborn class, and a thread that locks on the Stubborn.class instance, are locking on the same object.
Ai Ayumi wrote:
If only one lock, Does that mean if t1 is using synchronize(Stubborn.class), t2 won't be able to access static synchronized void push()?
Two different threads can't own the same lock at the same time.
Ai Ayumi wrote:
Also, the output prints 5 4 3 2 1 (by t1), then 0 (by t2). Why is it not alternatingly by t1, t2?
Take a look at the code. You will see that one thread is not releasing the lock -- so alternating is not possible.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Pritish Chakraborty
Ranch Hand
Joined: Jun 12, 2012
Posts: 91
|
|
What Henry said.
Additionally, sleep() will not release object lock. wait() could though.
|
OCJP 6
|
 |
Ai Ayumi
Greenhorn
Joined: May 26, 2012
Posts: 6
|
|
Henry Wong wrote:
Ai Ayumi wrote:
Aren't synchronize(Stubborn.class) and static synchronized void push() locked seperately?
Synchronized static methods use the instance of Class class (that represents the class which the method is declared) as its lock. So, a thread that calls a synchronized static method of the Stubborn class, and a thread that locks on the Stubborn.class instance, are locking on the same object.
Henry
Ok I see....
Does this also occur in non-static synchronizations? If one has synchronized block synchronized(this){} and a synchronized method(), assuming using the same object, if one Thread accesses the Block, another can't access the synchronized method at the same time?
|
 |
Pritish Chakraborty
Ranch Hand
Joined: Jun 12, 2012
Posts: 91
|
|
If I know my basics right, the following -:
is equivalent to
If I recall, this is given in K&B, Chapter-9. Being non-static, both synchronize on the object that started the thread (this refers to that object).
So yes, the other thread is blocked from accessing the lock while one thread is executing in its critical section.
|
 |
Cyril Sadasivan
Greenhorn
Joined: Jul 17, 2012
Posts: 20
|
|
|
Are questions like 'will the following deadlock?' expected on OCPJP 6 exam?
|
 |
 |
|
|
subject: Why won't the following Deadlock? Exam 2 q.16
|
|
|