Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Optimizing Singleton Design Pattern

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Optimizing Singleton Design Pattern

    Hi,

    Given a singleton class, if too many threads start accessing the class, the performance will get hampered. SO how do we optimize the code for this?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Optimizing Singleton Design Pattern

    If you're not modifying the singleton (either externally or internally) then theoretically every thread can access the singleton mutually with no negative effects.

    If you're able to partition the singleton into "sections" (for example, you have a 2D array and each thread operates on only a small block of the array), then again you can safely access the singleton without requiring synchronization.

    If your threads require a complete lock on the singleton (or a partial lock that all threads need), then there's no optimization which can take place because at most only 1 thread can safely access the singleton. This is a design bottleneck and no amount of optimization can fix this. The only option is to redesign your application to avoid this case.

    Without a more specific example (aka code) it's difficult to determine how to get more performance out of your code, or if this is even required.

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Optimizing Singleton Design Pattern

    Here is my singletonclss code
    class SingletonClass {
     
    	private static SingletonClass singletonObject;
    	/** A private Constructor prevents any other class from instantiating. */
    	private SingletonClass() {
    		//	 Optional Code
    	}
    	public static synchronized SingletonClass getSingletonObject() {
    		if (singletonObject == null) {
    			singletonObject = new SingletonClass();
    		}
    		return singletonObject;
    	}
    	public Object clone() throws CloneNotSupportedException {
    		throw new CloneNotSupportedException();
    	}
    }
    public class SingletonObjectDemo {
     
    	public static void main(String args[]) {
    		//		SingletonClass obj = new SingletonClass();
                    //Compilation error not allowed
    		SingletonClass obj = SingletonClass.getSingletonObject();
    		// Your Business Logic
    		System.out.println("Singleton object obtained");
    	}
    }
    Last edited by helloworld922; April 18th, 2011 at 07:58 PM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Optimizing Singleton Design Pattern

    On thing I can see right away is to get rid of the synchronization, as well as immediately initializing the singleton object (use a static initializer block, or just initialize it when you declare it). You're getter method can then simply return the singleton object without checking to see if it's null and creating the singleton object. This will allow multiple threads to retrieve the singleton object at the same time. However, there's no way for me to know how you're using that object which would say whether you can safely use that object in multiple threads at the same time.

  5. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Optimizing Singleton Design Pattern

    I would agree with removing the synchronized keyword and rather instantiate your singleton directly at the static declaration.

    You should really try to avoid using synchronized if there is no need as it just complicates the code but it's not always possible to do that.

    If you leave the code as it is, it will be efficient enough, the getSingletonObject method does not really impose a high performance risk since all it does is return a value.

    I myself prefer to use Google Guice these days so I don't see these issues, I never really need to call new on any class except for the odd rare occasion

  6. The Following User Says Thank You to Json For This Useful Post:

    tcstcs (April 19th, 2011)

Similar Threads

  1. Help in possible Design UI
    By beni.kurniawan in forum AWT / Java Swing
    Replies: 1
    Last Post: April 15th, 2011, 10:51 AM
  2. API singleton theory
    By dunnkers in forum Java Theory & Questions
    Replies: 10
    Last Post: February 24th, 2011, 12:57 PM
  3. How to Use an Observer Pattern
    By copeg in forum Java Programming Tutorials
    Replies: 0
    Last Post: February 9th, 2011, 10:24 PM
  4. Class design
    By arithmetics in forum Object Oriented Programming
    Replies: 4
    Last Post: November 4th, 2010, 08:44 AM
  5. Singleton pattern: the purpose of empty constructor
    By Asido in forum Java Theory & Questions
    Replies: 1
    Last Post: August 6th, 2010, 08:58 AM