You are on page 1of 5

6/12/13

A static synchronized method and a non-static synchronized method will not block each other, ever. (SCJP forum at JavaRanch)

Big Moose Saloon


A friendly place for programming greenhorns!
Search

Java FAQ

Recent Topics

Register / Login

A special promo: Enter your blog post or vote on a blogger to be featured in an upcoming Journal

JavaRanch Java Forums Certification Programmer Certification (SCJP/OCPJP)

Author

A static synchronized method and a non-static synchronized method will not block each other, ever.
posted 1/23/2012 1:17:02 PM

Prince Sewani Ranch Hand Joined: Nov 24, 2010 Posts: 32

Hi all, The line in the subject is on page 740 of SCJP- JAVA 6 -Exam study guide by Kathy Sierra &* Bert Bates, Can some-one please provide an example that justifies it? It will be of great help.

Regards Prince --------------------------------------------------------SC JP 6 , still a life long learner Anayonkar Shivalkar Bartender Joined: Dec 08, 2010
www.coderanch.com/t/565232/java-programmer-SCJP/certification/static-synchronized-method-static-synchronized 1/5

posted 1/23/2012 6:04:48 PM

Hi Prince,

6/12/13

A static synchronized method and a non-static synchronized method will not block each other, ever. (SCJP forum at JavaRanch)

Posts: 1295
I like...

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, Anayonkar Shivalkar (SC JP, SC WC D, OC MJD)

Prince Sewani Ranch Hand Joined: Nov 24, 2010 Posts: 32

posted 1/23/2012 7:55:17 PM

Hi Anayonkar, Thanks for the wonderful explanation, I understand all this, What I'm actually looking for is a practical working example that justifies this scenario, I tried a couple of things but couldn't think of an exact design that will prove it.

A nayonkar 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
www.coderanch.com/t/565232/java-programmer-SCJP/certification/static-synchronized-method-static-synchronized 2/5

6/12/13

A static synchronized method and a non-static synchronized method will not block each other, ever. (SCJP forum at JavaRanch)

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.

Anayonkar Shivalkar Bartender Joined: Dec 08, 2010 Posts: 1295


I like...

posted 1/23/2012 8:15:18 PM

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

www.coderanch.com/t/565232/java-programmer-SCJP/certification/static-synchronized-method-static-synchronized

3/5

6/12/13

A static synchronized method and a non-static synchronized method will not block each other, ever. (SCJP forum at JavaRanch)

it is necessary to use both static and non-static synchronized methods

Prince Sewani Ranch Hand Joined: Nov 24, 2010 Posts: 32

posted 1/25/2012 2:04 PM

Hi Anayonkar, Well, As I said earlier, I know all this, I was just looking for a practical example. I got what you drawn out of the debit-credit example. I found a working example for this in one of the Self Test Questions, here it is : public class ThreadDemo { synchronized void a() { actBusy(); } static synchronized void b() { actBusy(); } static void actBusy() { try { Thread.sleep(1000); } catch (InterruptedException e) {} } public static void main(String[] args) { final ThreadDemo x = new ThreadDemo(); final ThreadDemo y = new ThreadDemo(); Runnable runnable = new Runnable() { public void run() { int option = (int) (Math.random() * 4); switch (option) { case 0: x.a(); break; case 1: x.b(); break; case 2: y.a(); break; case 3: y.b(); break; } } }; Thread thread1 = new Thread(runnable); Thread thread2 = new Thread(runnable); thread1.start(); thread2.start(); } } About the scenario,when It is necessary to use both.. I'll get back to you on that..

A nayonkar Shivalkar wrote:

Well, Prince, to understand this at code level, you can take the banking example from Sierra & Bates' SC JP 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
www.coderanch.com/t/565232/java-programmer-SCJP/certification/static-synchronized-method-static-synchronized 4/5

6/12/13

A static synchronized method and a non-static synchronized method will not block each other, ever. (SCJP forum at JavaRanch)

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

I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.

subject: A static synchronized method and a non-static synchronized method will not block each other, ever.

Similar Threads static and non-static synchronized methods regarding static method synchronized synchronized(ObjectInstance){ //satements } AND Polymorphism An abstract method can NOT be declared static, synchronized,
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jun 12, 2013 08:52:21 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/565232/java-programmer-SCJP/certification/static-synchronized-method-static-synchronized

5/5

You might also like