• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1478
  • Last Modified:

Thread Safe Singleton?

I am looking at creating a thread safe singleton, I have put PMD Rules in place and my code (See below)

When I run the PMD Rules it's saying that :

double checked locking is not thread safe in java

and that my singleton is not thread safe.

Does anyone have a thread safe singleton or links to one?

Cheers



public class LogRegistry {

	private static LogRegistry instance = null;
	private static Object syncObject;

	private LogRegistry()
	{
	}

	public static LogRegistry getLogRegistry()
	{
if(instance == null)
		{
		synchronized(syncObject)
		{
			if(instance == null)
			{
				instance = new LogRegistry();
			}
		}
}

		return instance;
	}
}

Open in new window

0
directxBOB
Asked:
directxBOB
  • 6
  • 4
  • 2
  • +1
3 Solutions
 
ChristoferDutzCommented:
You are using a syncObject of value null ...
I usually have a String as syncObject. As long as this is 0 I doubt synchronization will work at all.
public class LogRegistry {

        private static LogRegistry instance = null;
        private static Object syncObject = "mySyncObject";

        private LogRegistry()
        {
        }

        public static LogRegistry getLogRegistry()
        {
if(instance == null)
                {
                synchronized(syncObject)
                {
                        if(instance == null)
                        {
                                instance = new LogRegistry();
                        }
                }
}

                return instance;
        }
}

Open in new window

0
 
HegemonCommented:
You can add another class, LogRegistryFactory, in which you will create your singleton in static initialisation. Thus: 1) you are guaranteed to have the only instance of the registry; 2) you separate the concerns of LogRegistry creation and use; 3) No need in synchronisation.

public class LogRegistryFactory {

	private static LogRegistry instance = new LogRegistry();

	public static LogRegistry getLogRegistry() {
             return instance;
        }
}

Open in new window

0
 
ChristoferDutzCommented:
The level to which this approach still is not 100% thread safe is relatively minimal. The only problem you may have is that if Thread A currently is creating "instance" and Thread B is called while the object is still created. It could happen, that the memory has allready been assigned to the varieable (and therefore it is not null) but the constructor is not finished initializing, so using it could result in errors.

One solution would be not to initialize the "instance" lazyly. If you create it as soon as the class is loaded you won't have any problems with it. Another solution would be method level synchronization, but synchronization is extremely expensive.

Here's a good article about the problem:
http://www.javaworld.com/javaworld/jw-02-2001/jw-0209-double.html?page=4

The following code should be quite thread-safe.
public class LogRegistry {

        private static final LogRegistry instance = new LogRegistry();

        private LogRegistry()
        {
        }

        public static LogRegistry getLogRegistry()
        {
            return instance;
        }
}

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ksivananthCommented:
you can achieve thread safe lazy loading singleton using an inner class,

public class LogRegistry {

       
        private LogRegistry()
        {
        }

        public static LogRegistry getLogRegistry()
        {
            return LogRegistryInstanceHolder.instance;
        }

   private class LogRegistryInstanceHolder{
private static final LogRegistry instance = new LogRegistry();

   }
}
0
 
directxBOBAuthor Commented:
@ChristoferDutz: unfortunately the double-checked locking isn't thread safe in java.

@Hegemon: like this idea and will keep in mind if this project expands further.

@ChristoferDutz: this is what I am leaning towards now, simply create a static final instance the second it gets called. My question is, if I do it this way do I really need a getLogRegistry?

as

LogRegistry.SomeMethod() would do the exact same thing?


@ksivananth: I think that ChristoferDutz method above may work easier. Not that there is a huge overhead in creating an inner class, it just seems redundant?

New to java so how the JVM and compiler works is all new.

Cheers


0
 
ChristoferDutzCommented:
Well you asked for a Singleton ;-) ...

of course you may use static methods. But I guess it depends on the thread-Safety of the methods you are using. The above pattern only avaoids concurrency problems when retrieving instances, if the methods of the instance are not thread safe,you will get problems there.
0
 
ksivananthCommented:
>>it just seems redundant?

it depends, if you really require( based on the complexity of creation and usage ) lazy loading, thats the way without sync.
0
 
ChristoferDutzCommented:
I think there is no real difference between ksivananths and my solution. The only thing is that instance variable and getInstance method are in two classes in his approach and in one in mine. Anyway I think I wouldn't have posted mine if I had allready seen his solution when I started writing ;-)
0
 
ChristoferDutzCommented:
@ksivananth the problem with your approach in 33742520 is that the synchronized block is allways executed. Synchronization is an extremely expensive operation (I have read about 100 times the overhead of a normal method call). Thats the main reason for the double checked locking. By synchronizing the access method you could skip the holder class completely and simply use the classic Singleton pattern. But if you have to gain access to an instance of LogRegistry often from different threads, this will get you into realy big performance trouble.
0
 
ksivananthCommented:
>> think there is no real difference between ksivananths and my solution.

there is a big difference, yours will create the instance when the class loaded into memory, mine will create only when the getinstance accessed!

>>@ksivananth the problem with your approach in 33742520 is that the synchronized block is allways executed.

there is not synchronization block!

>>Synchronization is an extremely expensive operation (I have read about 100 times the overhead of a normal method call).

I know, thats why suggested the inner class approach

>>Thats the main reason for the double checked locking

DCL is something you should never use, thats the reason for the big article in JavaWorld!
0
 
ChristoferDutzCommented:
Lol .. sorry for that .. I was refering to the solution of Hegemon ... didn't see that this was somebody different that posted the possible solution.

Ok ... and you are correct ... but to be 100% precise ... to me it looks as if you are not immune against concurrency issues against first access of the component from two threads simultaneously. Do you have any References stating that the solution you posted actually works as you stated it? I could read that none of the solutions that do lazy-loading actually work. in the JavaWorld article they state that eager initialization currently seems to be the only 100% safe method.
0
 
ksivananthCommented:
the idea is that static inner class is loaded only when referenced which we do here only in the getInstance! here you go http://c2.com/cgi/wiki?DoubleCheckedLockingIsBroken
0
 
directxBOBAuthor Commented:
Thanks for all the help, split the points over the 3 solutions that I tested with. I like the factory idea but will probably opt for the one of the other 2 options once I see what testing throws up.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now