The Artima Developer Community
Sponsored Link

Java Answers Forum
Singleton

9 replies on 1 page. Most recent reply: Jun 10, 2002 3:16 PM by Thomas SMETS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 9 replies on 1 page
JB

Posts: 1
Nickname: johnb
Registered: Jun, 2002

Singleton Posted: Jun 6, 2002 8:29 AM
Reply to this message Reply
Advertisement
Hi,

I want know about the JVM how the method area will work.

The question is if i create a singleton object,then how it is going to process in jvm.If i have 20000 users they call the methods on that object,how can this single object is going to serve the purpose.If so why is this object pooling.

I know the answer that it slow down the system,but i want a scenario,how it executes in JVM.

thanks in advance.

JB


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Singleton Posted: Jun 6, 2002 4:18 PM
Reply to this message Reply
I am not quiet sure I grasp the full meaning of your question John ... !

What else than "saying that the unique instance will have many references to it shared accross the VM" ?


Thomas SMETS,
SCJP2 - Brussels

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Singleton Posted: Jun 6, 2002 6:43 PM
Reply to this message Reply
You didn't read the GOF book (http://www.artima.com/designtechniques/booklist.html) carefully -- the singleton pattern is only guaranteed to work for up to 19999 users!

Seriously though, I think a single object is quite happy to handle 20000 or more users. Even if you divided the work among several objects, if they are all running on the same JVM, or even on the same machine, how would that help matters?

gopi

Posts: 5
Nickname: gopi
Registered: Jun, 2002

Re: Singleton Posted: Jun 7, 2002 9:25 AM
Reply to this message Reply
Hi,

Yes I think multi threads can not run same line of code in method area right.

Can u clarify please

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Singleton Posted: Jun 7, 2002 11:35 PM
Reply to this message Reply
Sure they can. However, if the method or section is synchronized, multiple threads can't run it at the same time; they will be blocked, which will result in them running that piece of code serially, rather than parallelly (hmm, I'm certainly not so sure about the etymological authenticity of my adverbiage here). I don't know about the singleton you are dealing with, but, of course, the best design would be to limit the synchronization to just where it is needed. But of course! On the other hand, if a method is not synchronized, many threads could be mingling in the middle of it simultaneously and in many cases, this is perfectly acceptable.

Marty Warnett

Posts: 1
Nickname: wibbleccfc
Registered: Jun, 2002

Re: Singleton Posted: Jun 9, 2002 9:15 AM
Reply to this message Reply
The synchronization issue is a major problem with Singletons it would appear - search through www.javaworld.com, numerous articles about it.

The problem is that the Singleton pattern is obviously used to ensure you can create one and only one actual instance of the Singleton class - to do this, you store a static reference to the class, when requesting the instance create it if the reference is null.

Using multithreading, however, you get a bit issue if an interrupting occurs between the time of the if (instance == null ) test and the creation.

Not too sure if anybody has a 100% working version, but ...

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Singleton Posted: Jun 9, 2002 10:08 AM
Reply to this message Reply
Why do you need to ever check whether the instance is null? If no one can access it, that shouldn't happen:

class Singleton
{
    private static final Singleton instance = new Singleton();   
 
    private Singleton()
    {
    }
 
    /**
     * Get a reference to the singleton <code>Singleton</code> instance.
     */ 
    public static Singleton getInstance() 
    {
        return instance;
    }
}

gopi

Posts: 5
Nickname: gopi
Registered: Jun, 2002

Re: Singleton Posted: Jun 10, 2002 8:53 AM
Reply to this message Reply
Hi,

why should we go for object pooling at all,if X(Singleton)Oject's Y(some method)method's Z line of code reference can be executed by several threads at a time in single JVM.

As Matt Gerrans(post221)if singleton object will be enough for 20000 users,how does this calculation comes in to picture.
My assuption is for object pooling is for one object may not be enough to serve more number of threads.

That means singleton object's Y method's Z line of code reference can not be execute in JVM Method Area parallelly.

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Singleton Posted: Jun 10, 2002 2:57 PM
Reply to this message Reply
I am currently on a project where we adopted the following structure :
Any class is instanciated from the environment via a ClassManager. The Class is therefore not reponsible for it's instanciation in a Singelton mode or not.
Firsly I would advise you to look deeply at the following links :
_ http://www.diasparsoftware.com/portfolio.html
Look at the "Use your singletons wisely"
_ http://www-106.ibm.com/developerworks/webservices/library/co-single.html

My advise from there is to bring forward the typical Singleton :
package test.lang.design_pattern.creational.singleton;
 
/**
 * Singleton:
 * Defines an instance operation that allows clients to access it's single instance
 * @author ben@javacoder.net
 * @date January 2002
 */
public class Singleton 
{
  private static Singleton instance; // own instance
 
  /* protected to enable controlled subclassing */
  protected Singleton() 
  {
  }
 
  public static Singleton getInstance() 
  {
    // 'lazy' evaluate instance
    if (instance == null)     
      instance = new Singleton();
 
    return instance;
  }
 
  public void operation() 
  {
    System.out.println("Singleton.operation() executing" );
  }
}

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Singleton Posted: Jun 10, 2002 3:16 PM
Reply to this message Reply
I am currently on a project where we adopted the following structure :
Any class is instanciated from the environment via a ClassManager. The Class is therefore not reponsible for it's instanciation in a Singelton mode or not.
Firsly I would advise you to look deeply at the following links :
_ http://www.diasparsoftware.com/portfolio.html
Look at the "Use your singletons wisely"
_ http://www-106.ibm.com/developerworks/webservices/library/co-single.html

My advise from there is to bring one step further the typical Singleton :
package test.lang.design_pattern.creational.singleton;
 
/**
 * Singleton:
 * Defines an instance operation that allows clients to access it's single instance
 * @author ben@javacoder.net
 * @date January 2002
 */
public class Singleton 
{
  private static Singleton instance; // own instance
 
  /* protected to enable controlled subclassing */
  protected Singleton() 
  {
  }
 
  public static Singleton getInstance() 
  {
    // 'lazy' evaluate instance
    if (instance == null)     
      instance = new Singleton();
 
    return instance;
  }
 
  public void operation() 
  {
    System.out.println("Singleton.operation() executing" );
  }
}
with its client :
package test.lang.design_pattern.creational.singleton;
 
/**
 * Client:
 * @author ben@javacoder.net
 * @date January 2002
 */
public class Client {
 
  public static void main(String[] args) 
  {
    // use getInstance to obtain Singleton instance
    Singleton s = Singleton.getInstance();
 
    // use operation
    s.operation();
  }
}
To two classes now (Singleton & SingletonFactory) :
The Singletonnable file
package test.lang.design_pattern.creational.singleton.withFactory;
/**
  * The purpose of this class is to "do something".
  * 
  * @author  <a href="mailto:tsmets@altern.org">Thomas SMETS, Brussels</a>
  * @version 0.0.1 initial
  */
public class Singleton
{
/**
  * This class is prefereably used as a <code>SingletonFactory</code> via a 
  * call to the  <code>SingletonFactory</code>.
  *
  * @see SingletonFactory
  */
  public Singleton ()
  {
    System.out.println ("public Singleton ()");
  }
  
  public void action ()
  {
    System.out.println ("Let's do something");
  }
}
The Factory look is now like this :
package test.lang.design_pattern.creational.singleton.withFactory;
/**
  * The purpose of this class is to :
  * _ Help the Singleton class to become a real Singleton
  * _ This class is itself a simple implementation Singleton
  *   (either recursive or simply "joking").
  * _ The Singleton really is not a Singleton but a possible Singleton
  *   The transfert to more than one instance of the class is almost 
  *   straight forward.
  * 
  * @author  <a href="mailto:tsmets@altern.org">Thomas SMETS, Brussels</a>
  * @version 0.0.1 initial
  */
public class SingletonFactory
{
  public static final SingletonFactory instance = new SingletonFactory ();
  public static final int INITIAL_SIZE = 5;
  
  public static Singleton[] refs = new Singleton[INITIAL_SIZE];
 
  private SingletonFactory ()
  {
    System.out.println ("private SingletonFactory ()");
  }
    
  public static SingletonFactory getInstance ()
  {
    System.out.println ("public static SingletonFactory getInstance ()");
    return instance;
  }
  
  public Singleton getSingleton ()
  {
    synchronized (refs)
    {
      if (refs[0] == null)
      {
        refs[0] = new Singleton ();
        return refs[0];
      } else 
        return refs[0];
    }
  }
}
The Client is almost not changed :
package test.lang.design_pattern.creational.singleton.withFactory;
 
/**
 * Client:
 * @author ben@javacoder.net
 * @date January 2002
 */
public class SingletonClient {
 
  public static void main(String[] args) 
  {
    // use getInstance to obtain Singleton instance
    Singleton s = SingletonFactory.getInstance().getSingleton ();
 
    // use operation
    s.action ();
  }
}

The great advantage of this is that the Factory is placed outside the class, itself... which make the code cleaner & just as efficient !

Hope this helps,
Tx to J. B. Rainsberger for his nice article.
Tx to Gino MARCKX to ahve forced us to use the Factories everywhere :-D

Thomas SMETS,
SCJP2 - Brussels

Flat View: This topic has 9 replies on 1 page
Topic: Can you create multicast sockets in an applet? Previous Topic   Next Topic Topic: Interface transitions

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use