Regards
Prince
---------------------------------------------------------
SCJP 6 , still a life long learner
Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Anayonkar Shivalkar wrote:Hi Prince,
To understand this statement, first, one should understand what is meaning of 'two methods block each other'.
This simply means that - on one object, two methods can never execute simultaneously. One method must wait till other method finishes.
How does this happen? Well, when two methods are object level (non-static) and synchronized, it is same has having a non-synchronized method with all lines of code inside synchronized(this) block.
So, when two threads try to call two synchronized method on same object, due to synchronization, only one thread will have object lock at a time and two threads cannot proceed concurrently.
But what if one method is static synchronized and another method is just synchronized (non-static)? At that time, even if two threads are executing two methods on same object, one thread (executing non-static method) acquires object level lock and proceed. Another thread (executing static method) acquires class level lock (i.e. synchronized(classname.class)) and proceeds (static methods are always class level even if called as obj.method()). And thus, a static synchronized method and a non-static synchronized method will not block each other, ever.
Hope this helps.
Regards
Prince
---------------------------------------------------------
SCJP 6 , still a life long learner
Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Anayonkar Shivalkar wrote:Well, Prince, to understand this at code level, you can take the banking example from Sierra & Bates' SCJP book.
There, both methods - debit and credit (don't remember the exact names) are non-static and synchronized. You can make 1 method static and see that the code is not thread safe anymore.
e.g. debit is static and credit is non-static. Both are synchronized.
One thread is debiting 5 and other thread is crediting 3. Initial balance is 10.
1) credit thread acquires lock on object, and enters in credit method.
2) debit thread acquires lock on class, and enters in debit method.
3) credit thread reads the balance as 10.
4) debit thread reads the balance as 10.
5) credit thread credits the balance and writes to account. balance is now 13.
6) debit thread has already red the balance as 10, so it will debit 5 and write balance to account. balance is now 5.
In thread safe environment, no matter in which order these operations happen, final balance should always be 8 (i.e. 10 + 3 - 5 or 10 - 5 + 3).
Point here, is not about static or non-static method. It is on which object you are acquiring lock. If both methods are static or non-static, then it is guaranteed that those will try to acquire lock on same object ('this', or classname.class) and the methods will never execute concurrently (on same object of course). But static and non-static combination is like those methods are trying to acquire lock on two different objects, so one method will not wait for another. Static synchronized methods are used to maintain thread-safety of static members of a class.
I hope this helps. Btw, you are welcome to share if you get any example where it is necessary to use both static and non-static synchronized methods
Regards
Prince
---------------------------------------------------------
SCJP 6 , still a life long learner
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |