File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes The static synchronized methods of the same class always block each other as only one lock per class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "The static synchronized methods of the same class always block each other as only one lock per class" Watch "The static synchronized methods of the same class always block each other as only one lock per class" New topic
Author

The static synchronized methods of the same class always block each other as only one lock per class

abhi chakraborty
Greenhorn

Joined: Feb 08, 2010
Posts: 2
I came across these statements

=> The static synchronized methods of the same class always block each
other as only one lock per class exists. So no two static synchronized
methods can execute at the same time.

=> When a synchronized non static method is called a lock is obtained on
the object. When a synchronized static method is called a lock is obtained
on the class and not on the object.


I wrote these simple programs to understand how exactly it works.

Program 1 :



Program 2 :



Program 3 :


Program 4 :



I expected this output:

Inside testSync1 ::::counter::::: 1
Inside nonStaticMethod1 ::::counter::::: 2 ::::count::::: 1

Inside nonStaticMethod2 ::::counter::::: 4 ::::count::::: 2

But, got this output :

Inside testSync1 ::::counter:::::1
Inside nonStaticMethod1 ::::counter:::::2::::count:::::1
Inside testSync2 ::::counter:::::3
Inside nonStaticMethod2 ::::counter:::::4::::count:::::2


Could you please explain the output?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

Abhi,
Welcome to the Ranch.

We have a forum dedicated to Threading and Synchronization. I will move this topic there for you.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Kuldip Shetty
Ranch Hand

Joined: Jan 07, 2010
Posts: 41

1. TestStaticSync.testSync1() - Obtains lock for all static synchronized methods of class TestStaticSync. TestStaticSync.testSync2() will be waiting for this lock to be released.

2. testStaticSync.nonStaticMethod1(); - Obtains lock for all instance methods which are invoked using the object reference testStaticSync.

3. TestStaticSync.testSync2() - As no Class level lock is present for static synchronized methods testSync2 obtains the class level lock.

4. testStaticSync.nonStaticMethod2(); - As object lock is released by nonStaticMethod1, nonStaticMethod2 obtains the lock.

Kuldip
abhi chakraborty
Greenhorn

Joined: Feb 08, 2010
Posts: 2
In that case if both the threads try to access TestStaticSync.testSync1() , Then the static synchronized methods of the same class should block each other as only one lock per class.

public class ThreadOne implements Runnable
{
TestStaticSync testStaticSync;
ThreadOne(TestStaticSync t)
{
this.testStaticSync = t;
new Thread(this,"ThreadOne").start();
}

public void run()
{
TestStaticSync.testSync1();
testStaticSync.nonStaticMethod1();
}
}

public class ThreadTwo implements Runnable
{
TestStaticSync testStaticSync;
ThreadTwo(TestStaticSync t)
{
this.testStaticSync = t;
new Thread(this,"ThreadTwo").start();
}

public void run()
{
TestStaticSync.testSync1();
testStaticSync.nonStaticMethod2();
}
}

But, in that case also I'm getting the same output.

Inside testSync1 ::::counter:::::1
Inside testSync1 ::::counter:::::2
Inside nonStaticMethod1 ::::counter:::::3::::count:::::1
Inside nonStaticMethod2 ::::counter:::::4::::count:::::2



Kuldip Shetty
Ranch Hand

Joined: Jan 07, 2010
Posts: 41

Its the same case as explained before.

1. TestStaticSync.testSync1() - Class level lock is obtained for all static synchronized methods and the second thread is waiting for the Class lock to be released to invoke TestStaticSync.testSync1(). Hence program output for synchronized methods are shown below

Inside testSync1 ::::counter:::::1
Inside testSync1 ::::counter:::::2

Kuldip
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: The static synchronized methods of the same class always block each other as only one lock per class