• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Synchoronizing a static method

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Synchoronizing a static method
public class A
{
public static synchronized void m1()
{
System.out.println("method1");
//some code here
}
public synchronized void m2()
{
System.out.println("method2");
//some code here
}
public static void main(String[] args)
{
A a = new A();
a.m2();
a.m1();
A b = new B();
b.m2();
b.m1();
}
}
??In the above code the static method m1 and non-static method m2 are synchronized.
I just need to know how a lock will be gained by a Thread calling method m1 since it is static and common to all instances.
Thanks in advance.
 
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
from the api documentation of java.lang.Object:


getClass
public final Class getClass()
Returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.
Returns:
the object of type Class that represents the runtime class of the object.
hashCode


cheers
karl
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anand Sidharth:
I just need to know how a lock will be gained by a Thread calling method m1 since it is static and common to all instances.
Thanks in advance.


The behavior of the Threads are unpredictable and they are managed by JVM to gain a lock... The algorithm is behind the scence...
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every class has a class Object. When a static method is synchronized it means that the thread must obtain lock on the class Object.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pradeep Bhat:
Every class has a class Object.


I'm not that sure what u mean by that... Do u mean "Every class is a class Object."
Correct me, if I'm wrong...
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ko Ko Naing:

I'm not that sure what u mean by that... Do u mean "Every class is a class Object."
Correct me, if I'm wrong...


Well, you would be correct if it were Smalltalk...
In Java, classes are *not* objects, but are associated to objects of the type java.lang.Class, which just hold meta data about the actual class.
One place where this difference becomes apparent is in class methods (static methods) not being polymorphic.
 
Life just hasn't been the same since the volcano erupted and now the air is full of tiny ads.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic