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 4 of 4

Thread: Why would somebody write a singleton like this?

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    22
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Why would somebody write a singleton like this?

    Why would somebody write a singleton like this (with volatile and synchronized)?

    public class Singleton {
        private volatile static Singleton singleton;
     
        private Singleton(){}
     
        public static Singleton getInstance(){
            if(singleton==null){
                synchronized (Singleton.class){
                    if(singleton==null){
                        singleton = new Singleton();
                    }
                }
            }
            return singleton;
        }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,844
    Thanks
    53
    Thanked 2,576 Times in 2,527 Posts

    Default Re: Why would somebody write a singleton like this?

    Try asking these unusual java program questions here: http://www.coderanch.com/forums
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2020
    Location
    Jacksonville, FL
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why would somebody write a singleton like this?

    Thanks for this link!

  4. #4
    Junior Member
    Join Date
    Apr 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why would somebody write a singleton like this?

    A good explanation of Singleton is here : https://javatechonline.com/singleton...all-scenarios/

Similar Threads

  1. How to clone a singleton class?
    By Pratyush in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 4th, 2013, 02:27 AM
  2. Question on Singleton class
    By tcstcs in forum Java Theory & Questions
    Replies: 0
    Last Post: September 26th, 2012, 07:48 AM
  3. About Spring Singleton
    By boosachandu in forum Web Frameworks
    Replies: 0
    Last Post: July 18th, 2012, 03:17 AM
  4. Subclassing Singleton class
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: December 22nd, 2011, 12:42 AM
  5. API singleton theory
    By dunnkers in forum Java Theory & Questions
    Replies: 10
    Last Post: February 24th, 2011, 12:57 PM