• Post Reply Bookmark Topic Watch Topic
  • New Topic

synchronized block in static method

 
sarvesh meens
Ranch Hand
Posts: 43
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A block of statements in a static method has to be locked.
snippet:
static void myMethod()
{
....
....
//this is the block to be locked
//synchronized(this) will not work here
//'this' is not available in static context
if(myReference == null)
{
myReference = defaultObj;
myReference.doSomething();
}
...
...
}

Have read that when a static method is made synchronized,the class's singular class-object is locked.
want to do the same here.

synchronized(getClassObject) //how to get class-object associated with this class?
{
if(myReference == null)
{
myReference = defaultObj;
myReference.doSomething();
}
}

There are other alternates(like using Locks in java.util.concurrent or creating explicit lock object).
But,want to know how the singular class-object associated with the class can be obtained and locked.
[ November 16, 2007: Message edited by: sarvesh meens ]
 
Rajah Nagur
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use <ClassName>.class
for example if your ClassName is UserAction; then use UserAction.class
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
synchronized static void myMethod(){
....
....
}


I fail to understand why you need to re-acquire the class object's lock since you already have it aquired by the thread once it enters the method synchronized static void myMethod().
 
sarvesh meens
Ranch Hand
Posts: 43
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by san ban:

synchronized static void myMethod(){
....
....
}

[/qb]


San,the method is not suppossed to be synchronized.
Corrected the original post.

Thanks Rajah.
Used .class to get class-object reference.

That provokes another question.
What is this .class thing?
It must be an attribute.
But it is not found in Object.
myArray.length is another such attribute.
Where do the these attributes come from?
[ November 16, 2007: Message edited by: sarvesh meens ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are both illusions created by the compiler. They're not really member variables of any class.

When the compiler sees ".class", it will insert code that uses a Class.forName() call to get the actual Class object, then caches it in a generated member variable.

When the Java compiler sees ".length", it emits the "arraylength" bytecode, which returns the length of an array. The length is stored with the array object in some implementation-dependent way which 'arraylength' knows how to find.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!