• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abhi,
Welcome to the Ranch.

We have a forum dedicated to Threading and Synchronization. I will move this topic there for you.
 
Ranch Hand
Posts: 45
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic