Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By JosAH

Thread: Why twice condition check?

  1. #1
    megabull is offline Member
    Join Date
    Aug 2012
    Posts
    22
    Rep Power
    0

    Default Why twice condition check?

    In the example below, why has synchronized been checked twice? I'm talking about lines 7 and 9 .

    Java Code:
    public class Singleton {
            private static volatile Singleton instance = null;
     
            private Singleton() {   }
     
            public static Singleton getInstance() {
                    if (instance == null) {
                            synchronized (Singleton.class){
                                    if (instance == null) {
                                            instance = new Singleton();
                                    }
                          }
                    }
                    return instance;
            }
    }
    Tools of Choice : Eclipse Helios | Windows Vista | Windows 7 | MySQL | Apache Tomcat

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: Why twice condition check?

    Because code executing on another Thread may have been creating the instance while this Thread was waiting for the lock in order to enter the synchronized block.

    That said, your code looks rather old. In early Java versions, this kind of coding was considered optimal because a null check was more than an order of magnitude faster than obtaining a lock. With improvements in both computers and the JVM, the whole method would probably be synchronized. If at all anyone is still using the Singleton (anti-) pattern.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: Why twice condition check?

    And syncrhonising is pointless for a singleton anyway:
    Java Code:
    public class MySingleton {
        private static final MySingleton instance;
        private MySingleton() {
        }
        public static MySingleton getInstance() {
            return instance;
        }
    }
    Also I wouldn't consider a singleton to be an anti-pattern at all.
    Abused, yes, but not an anti-pattern.

    Edit:
    Java Code:
    public class MySingleton {
        private static final MySingleton instance = new MySingleton();
        private MySingleton() {
        }
        public static MySingleton getInstance() {
            return instance;
        }
    }
    Last edited by Tolls; 09-11-2012 at 06:15 PM. Reason: Idiocy
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default Re: Why twice condition check?

    Quote Originally Posted by Tolls View Post
    Java Code:
    public class MySingleton {
        private static final MySingleton instance;
        private MySingleton() {
        }
        public static MySingleton getInstance() {
            return instance;
        }
    }
    Better call that class MyNullyton ;-)

    kind regard,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: Why twice condition check?

    Oops.
    :)

    I have a feeling I did the same thing on another thread earlier today.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. How to use class in if condition
    By raj.mscking@gmail.com in forum New To Java
    Replies: 8
    Last Post: 03-08-2012, 03:53 PM
  2. Condition ignored.
    By Pojahn_M in forum New To Java
    Replies: 8
    Last Post: 12-09-2011, 02:21 AM
  3. Waiting on a condition
    By nephos in forum New To Java
    Replies: 9
    Last Post: 04-22-2011, 10:05 AM
  4. basic condition
    By ts96 in forum New To Java
    Replies: 1
    Last Post: 02-16-2011, 12:23 PM
  5. While loop condition
    By counterfox in forum New To Java
    Replies: 3
    Last Post: 10-10-2010, 01:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •