Link to home
Start Free TrialLog in
Avatar of directxBOB
directxBOBFlag for Ireland

asked on

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

Avatar of ChristoferDutz
ChristoferDutz
Flag of Germany image

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

SOLUTION
Avatar of Hegemon
Hegemon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of directxBOB

ASKER

@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


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.
>>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.
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 ;-)
@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.
>> 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!
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.
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
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.