• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronizing on static references

 
Devendra Inamdar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How does synchronization work when a lock is acquired on an object which is pointed by a static reference? Is it a Class level lock because reference is static or an Object level lock because object is allocated from heap?

Here is an example of typical singleton

public class SingletonTest {

private Hashtable offerStatusCache = new Hashtable();

private static SingletonTest _cacheInstance = null;

private SingletonTest (){
}

static{
_cacheInstance = new SingletonTest ();
}

public static SingletonTest getInstance(){
return _cacheInstance;
}
}

SingletonTest test = SingletonTest.getInstance()

Which lock is used when this statement is executed

synchronized(test){
test.doSomething();
}

It's true that the effect would be same as class level lock since object is a singleton.


Thanks in Advance
Devendra
 
Angel Taveras
Ranch Hand
Posts: 84
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's almost the same, but not quite. It will be different when you have the same class loaded in multiple classloader inside the same vm. The Static it's only static within a classloader.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!