Java multi thread singleton

I just want to confirm since singleton only has one instance, it is guranteed that my business logic method could be executed as thread safe. Am I right?
public class SingletonTest{
   private static SingletonTest instance = new SingletonTest();

   private SingletonTest{
     ****
   }  

   public static SingletonTest getInstance(){
               return instance;          
   }  

   public void SomebusinessLogicMethod(){
      /*  Ha, I am going to access some shared resources */
   }  
}

Open in new window

JianJunShenAsked:
Who is Participating?
 
ksivananthConnect With a Mentor Commented:
>>The authors implementation is different

true, its thread safe.

But in webapp env, we may not assume that there will be only one instance created because there can be multiple classloader which will cause multiple instance though it is static. if you try to load same class through two different classloader, there will be two instance of the same class created because a class loaded by diff classloader is not treated equally.
0
 
Gurvinder Pal SinghConnect With a Mentor Commented:
Singleton pattern ensures that you have only one object/resource.
You can implement thread safe behavior by using synchronized methods and synchronized blocks
some good reads
http://www.ibm.com/developerworks/java/library/j-dcl.html
http://java.sun.com/developer/technicalArticles/Programming/singletons/
0
 
rockiroadsCommented:
if you make the getInstance synchronized that would help make it thread safe I think

public static SingletonTest getInstance() {
    if( instance == null ) {
      synchronized( SingletonTest.class ) {
        if ( instance == null ) {
           instance = new SingletonTest();
        }
      }


0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
objectsConnect With a Mentor Commented:
depends on the environment you are running it in. for example if its a webapp then there could well be more than one instance.

Also that code is not thread safe
0
 
ValeriConnect With a Mentor Commented:
If you need only confirmation whether your class is thread safe, the answer is YES.
The implementation you have posted implements singleton pattern and it's thread safe!
0
 
ValeriConnect With a Mentor Commented:
objects why do you think it's not thread safe? Java static initializers are thread safe, and getInstance() don't need to be synchronized.
0
 
Gurvinder Pal SinghConnect With a Mentor Commented:
@Valeri: not answering on Objects behalf, but you can check the first link that i have posted in my reply to see why it is not thread safe
0
 
ValeriCommented:
@gurvinder372 The authors implementation is different. He has avoided lazy initialization of the instance object. this kind of initialization is thread safe :
private static SingletonTest instance = new SingletonTest();
0
 
objectsCommented:
access to SomebusinessLogicMethod() needs to be synchronized
0
 
Gurvinder Pal SinghConnect With a Mentor Commented:
@Valeri: that is correct, the object created will be only one, but it can still be accessed by multiple threads.
So other methods needs to be synchronized.
0
 
ValeriConnect With a Mentor Commented:
Synchronization of SomebusinessLogicMethod is not needed, only access to the shared resources is needed.
>> So other methods needs to be synchronized.
getInstance() don't need to be syncronized.

0
 
ValeriConnect With a Mentor Commented:
About the environment with multiple class loaders I absolutely agree with ksivananth and objects.
0
 
JianJunShenAuthor Commented:
I summarize answer here: my example of creating instance is thread safe, but business logic method is not thread safe. Am I right? If I am right, I will close this question.
0
 
HegemonConnect With a Mentor Commented:
Your singleton guarantees that the business method will be invoked on the only instance of this class (per classloader), nothing more. The method itself is NOT thread safe, it is an ordinary method. To make it thread safe, access to shared resources must be synchronized. Either the whole method can be declared as synchronized or a block of code inside it.
0
 
ksivananthConnect With a Mentor Commented:
>>Am I right?

yes, if its loaded through single classloader
0
 
JianJunShenAuthor Commented:
I did not give Rockiroads points is because it is not documented as thread safe in some Java memeory model. Please refer to gurvinder372 thread.
0
 
rockiroadsCommented:
yea okay. I just gave a simple example based on your code about using synchronise. I am aware that you can have multiple copies. Problem with the way I suggested would mean you need to sync all access to the variable that could be written by multiple threads. That goes some way towards making it thread safe but probably not so elegant way of doing things.
Then again I could be wrong but that is what I have been led to believe.
0
 
ksivananthCommented:
>>That goes some way towards making it thread safe but probably not so elegant way of doing things.
 
actually its not 100% safe, it will encounter the DCL( double checked locking ) issue
0
 
rockiroadsCommented:
so if everything is synchronized, dcl would be an issue? well maybe but I read it different from here
http://www.javaworld.com/javaworld/jw-02-2001/jw-0209-double.html?page=4

The most effective way to fix the DCL idiom is to avoid it. The simplest way to avoid it, of course, is to use synchronization. Whenever a variable written by one thread is being read by another, you should use synchronization to guarantee that modifications are visible to other threads in a predictable manner.



0
 
ksivananthCommented:
>>so if everything is synchronized, dcl would be an issue?

no, if the entire method is sync'ed, it would be thread safe but in your comment, you were checking null and then sync'ed which will cause DCL
0
 
rockiroadsCommented:
ah, didnt see that. the instance null check done after as well, not sure why I did 2, anyways, thanks for that spot.
0
All Courses

From novice to tech pro — start learning today.