Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

why this is used in synchronized block in singleton  RSS feed

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!