aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A static synchronized method and a non-static synchronized method will not block each other, ever. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A static synchronized method and a non-static synchronized method will not block each other, ever." Watch "A static synchronized method and a non-static synchronized method will not block each other, ever." New topic
Author

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

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
---------------------------------------------------------
SCJP 6 , still a life long learner
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1510
    
    5

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,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Prince Sewani
Ranch Hand

Joined: Nov 24, 2010
Posts: 32
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.


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.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1510
    
    5

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
Prince Sewani
Ranch Hand

Joined: Nov 24, 2010
Posts: 32
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..

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
 
Consider Paul's rocket mass heater.
 
subject: A static synchronized method and a non-static synchronized method will not block each other, ever.