Board index » jbuilder » Singleton pattern

Singleton pattern


2004-07-28 10:59:39 PM
jbuilder24
Dear expert<
Is the following version of singleton pattern thread-safe? If it is not,
why?
public class Singleton {
private static final instance = new Singleton();
private Singleton() {
//here may be simple initialization of non static fields.
}
public static Singleton getInstance() {
return instance;
}
}
It seems to be not thead-safe but I am not sure.
Best regards,
Alexei Akimov.
 
 

Re:Singleton pattern

If the non-static fields are read-only, then the class is thread-safe. If
you will be modifying anything in the object, then the modifications need to
be protected with the "synchronized" keyword. A good book on the subject of
threading and synchronization is "Java Threads":
www.amazon.com/exec/obidos/tg/detail/-/1565924185/qid=1091033358/sr=8-1/ref=pd_ka_1/002-1130274-0409661?v=glance&s=books&n=507846
--
Check out our latest white papers at
www.datadevelopment.com/papers/index.html
BladeNET Scores With Borland Enterprise Tools
Team Development with JBuilder and Borland Enterprise Server
Dolphin Data Development Ltd.
www.datadevelopment.com/
"Alexei A. Akimov" < XXXX@XXXXX.COM >wrote in message
Quote
Dear expert<

Is the following version of singleton pattern thread-safe? If it is not,
why?

public class Singleton {

private static final instance = new Singleton();

private Singleton() {
//here may be simple initialization of non static fields.
}

public static Singleton getInstance() {
return instance;
}
}

It seems to be not thead-safe but I am not sure.

Best regards,
Alexei Akimov.

 

Re:Singleton pattern

Alexei A. Akimov wrote:
Quote
It seems to be not thead-safe but I am not sure.
To add to Kevin's reply:
* The _creation_ of the singleton is thread-safe, because it's done
during class loading, which is sychronized in the class loader. (Two
threads can't load the class independently, in parallel, if they are
using the same class loader).
* But _access_ to the singleton object (including accessing its member
data) must be made thread-safe by you.
If you are only exposing read access to the members, it's quite likely
already safe.
However, if calls to the member methods of the singleton object can
modify the internal state (either explicit setXXX()-like calls, or
methods that perform some internal caching or other writes), then they
need to be properly synchronized by you (by putting synchronized on the
method definitions, or otherwise doing synchronizations inside the object).
 

{smallsort}

Re:Singleton pattern

First of all, thank You!
Quote
* The _creation_ of the singleton is thread-safe, because it's done
during class loading, which is sychronized in the class loader. (Two
threads can't load the class independently, in parallel, if they are
using the same class loader).
Would You please, give me some link to specification where this fact is
stated.
I looked through Lava Language Specification 2.0 but the only thing I
found was chapter 12.4.2. They say that a class loader put lock on the
class object twice: before initializing fields and after it. So it seems
that there no locks while initializing class fields.
I think You are right, I tested my code on Sun JVM 1.4.2 for Linux and
it was OK, but some formal specification is highly desired.
Best regard,
Alexei Akimov
 

Re:Singleton pattern

Alexei A. Akimov wrote:
Quote
I looked through Lava Language Specification 2.0 but the only thing I
found was chapter 12.4.2. They say that a class loader put lock on the
class object twice: before initializing fields and after it. So it seems
that there no locks while initializing class fields.
If you read the section in question
(java.sun.com/docs/books/jls/second_edition/html/execution.doc.html)
it says *exactly* what you're looking for.
In Step 6, you first mark the class as "initialization in progress"
before releasing the lock.
So the next thread that ends up in step 2 (after waiting for the lock on
step 1), must wait until the initialization is over (by giving up *its*
lock and sleeping). So even though the second thread can get to step 2,
it can't get any further, until the class is marked as "initialization
completed".
 

Re:Singleton pattern

Thank You.
Alexei Akimov.