aspose file tools
The moose likes Threads and Synchronization and the fly likes why this is used in synchronized block  in singleton Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "why this is used in synchronized block  in singleton" Watch "why this is used in synchronized block  in singleton" New topic
Author

why this is used in synchronized block in singleton

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
senerio -I:
public static MySingleton getInstance() {
if (_instance==null) {
synchronized (MySingleton.class) {
if (_instance==null) {
_instance = new MySingleton();
}
}
}
}

I can understand everyhting but why we are using "MySingleton.class" in synchronized.


This is another senerio:

senerio -II :
public static MySingleton getInstance() {
if (_instance==null) {
synchronized (this) {
if (_instance==null) {
_instance = new MySingleton();
}
}
}
}

I can understand everyhting but why we are using "this" in synchronized.



thanks

Sai


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
Please use the code button; you ought to know about it by now. I think this is a threads-related theme, which would sit better on the threads forum, so I shall move it.

The first example looks suspiciously like double-checked locking; I can't remember why, but double-checked locking doesn't work.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

The code in scenario 2 will not even compile. Reason being that you can not refer to "this" in a static method.

In Scenario 1:
The intention of the synchronized block is to make only one thread enter the synchronized block at any time. Class level lock(using .class as the monitor) is one such way of assuring this.
The other way can be to have some other static final field as the monitor.

Campbell: I can't remember why, but double-checked locking doesn't work.


DCL *was* broken prior to jdk 5. The new JMM has fixed the problem and rendered DCL as thread-safe. However, it still is not recommended as it does not really give much.
Much more detailed explanation and lot of good information in these excellent articles by Brian Goetz:

What is the Java Memory Model, and how was it broken in the first place?

How will the JMM change under JSR 133?


apigee, a better way to API!
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2770
    
    2
[Nitesh]: DCL *was* broken prior to jdk 5. The new JMM has fixed the problem and rendered DCL as thread-safe.

But only if you declare the _instance variable as volatile.
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
will this work in multi threading environment?




and what is the diffenrence in using :
1)final static private Test test =new Test();
2)private Test test =new Test();

Thanks
Sai
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Yes, that will work and it is the eagerely initialized version of the singleton.

and what is the diffenrence in using :
1)final static private Test test =new Test();
2)private Test test =new Test();


I think you missed the static keyword in 2).
Anyways, the above two options are not specific to DCL or singleton.
The difference is the same as between using final/static or not.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why this is used in synchronized block in singleton
 
Similar Threads
Related to Singleton class
difference between class with static method and singleton pattern
Need a single instance of object.
How to use Singleton Design pattern in a Clustered Environment?
Objects In Servlet Context