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

why this is used in synchronized block in singleton

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic