Can you implement a Singleton within J2ME Apps, without using ThreadLocal?

Discussion in 'Java' started by Steve Jasper, Nov 19, 2003.

  1. Steve Jasper

    Steve Jasper Guest

    Hi, somewhat of a J2ME newbie, but i've been writing java for a few
    years. I was trying to implement a Singleton object within a test J2ME
    application, but I typically use ThreadLocal to do this, which isn't
    available in the MIDP.

    Does anyone have any suggestions on how to Implement a singleton
    without the use of ThreadLocal?

    Thanks,
    -kerry
    Steve Jasper, Nov 19, 2003
    #1
    1. Advertising

  2. Steve Jasper wrote:

    > Hi, somewhat of a J2ME newbie, but i've been writing java for a few
    > years. I was trying to implement a Singleton object within a test J2ME
    > application, but I typically use ThreadLocal to do this, which isn't
    > available in the MIDP.
    >
    > Does anyone have any suggestions on how to Implement a singleton
    > without the use of ThreadLocal?
    >
    > Thanks,
    > -kerry


    Why do you use a ThreadLocal to make a singleton?
    Almost all versions I've seen go like this:

    public class A {

    private static A a;

    private A(){
    }

    public synchronized A getInstance() {
    if(a == null) {
    a = new A();
    }
    return a;
    }
    }



    --
    Regards,
    Christophe Vanfleteren
    Christophe Vanfleteren, Nov 19, 2003
    #2
    1. Advertising

  3. Steve Jasper wrote:

    > Hi, somewhat of a J2ME newbie, but i've been writing java for a few
    > years. I was trying to implement a Singleton object within a test J2ME
    > application, but I typically use ThreadLocal to do this, which isn't
    > available in the MIDP.
    >
    > Does anyone have any suggestions on how to Implement a singleton
    > without the use of ThreadLocal?


    Just implement the singleton. Then make sure that your either synchronize
    each method or use a semaphore object within the singleton to ensure each
    thread accessing it does so serially.

    --
    Darryl L. Pierce <>
    Visit the Infobahn Offramp - <http://mypage.org/mcpierce>
    "What do you care what other people think, Mr. Feynman?"
    Darryl L. Pierce, Nov 19, 2003
    #3
  4. Christophe Vanfleteren wrote:

    > Why do you use a ThreadLocal to make a singleton?
    > Almost all versions I've seen go like this:
    >
    > public class A {
    >
    > private static A a;
    >
    > private A(){
    > }
    >
    > public synchronized A getInstance() {
    > if(a == null) {
    > a = new A();
    > }
    > return a;
    > }
    > }


    I would instance code the singleton to initialize when the class is loaded,
    rather than doing it when the instance is requested. But, that's personal
    preference, unless there's an advantage to doing it this way.

    --
    Darryl L. Pierce <>
    Visit the Infobahn Offramp - <http://mypage.org/mcpierce>
    "What do you care what other people think, Mr. Feynman?"
    Darryl L. Pierce, Nov 19, 2003
    #4
  5. Christophe Vanfleteren wrote:

    > Steve Jasper wrote:
    >
    >> Hi, somewhat of a J2ME newbie, but i've been writing java for a few
    >> years. I was trying to implement a Singleton object within a test J2ME
    >> application, but I typically use ThreadLocal to do this, which isn't
    >> available in the MIDP.
    >>
    >> Does anyone have any suggestions on how to Implement a singleton
    >> without the use of ThreadLocal?
    >>
    >> Thanks,
    >> -kerry

    >
    > Why do you use a ThreadLocal to make a singleton?
    > Almost all versions I've seen go like this:
    >
    > public class A {
    >
    > private static A a;
    >
    > private A(){
    > }
    >
    > public synchronized A getInstance() {
    > if(a == null) {
    > a = new A();
    > }
    > return a;
    > }
    > }
    >
    >
    >


    Erm, that should be "public static synchronized A getInstance()" ofcourse :)

    It's especially embarassing since such an example came up in the "signs of
    stupid java code" thread :)

    --
    Regards,
    Christophe Vanfleteren
    Christophe Vanfleteren, Nov 20, 2003
    #5
  6. Steve Jasper

    Steve Jasper Guest

    Great, thanks for the help. I've looked over some earlier code and
    realized that the ThreadLocal object I was using was absolutely
    extraneous as you guys pointed out. Time to dust off the old design
    pattern book.

    Thanks again...
    Steve Jasper, Nov 20, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. robin
    Replies:
    0
    Views:
    844
    robin
    Jul 20, 2003
  2. sods
    Replies:
    0
    Views:
    397
  3. aph
    Replies:
    4
    Views:
    390
    Peter Hansen
    Jan 14, 2006
  4. Steven Woody
    Replies:
    2
    Views:
    309
    Jay Nabonne
    Dec 29, 2005
  5. Charles Oliver Nutter

    Singleton methods without the singleton class

    Charles Oliver Nutter, Mar 15, 2010, in forum: Ruby
    Replies:
    4
    Views:
    195
    Charles Oliver Nutter
    Mar 22, 2010
Loading...

Share This Page