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

S

Steve Jasper

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
 
C

Christophe Vanfleteren

Steve said:
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;
}
}
 
D

Darryl L. Pierce

Steve said:
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.
 
D

Darryl L. Pierce

Christophe said:
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.
 
C

Christophe Vanfleteren

Christophe said:
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 :)
 
S

Steve Jasper

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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top