• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

About Singleton pattern, why this happen?

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Singleton class named TaskListBean, it designed as following:


I execute it at two place:
one: TaskStatusAction

this bean is instance variable.
two: TaskOAL

this bean is class variable.

When I print bean1 and bean2, it show two instance of TaskListBean:


I don't know why this happen, my environment is web application, please help me !! thanks !!
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Leo,

I am not able to make out the problem. I suggest you put some more code. However, in the meanwhile if your object creation is multithreaded, synchronize the getInstance() method. Hope this helps.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the result of comparing the references?

System.out.println(bean1 == bean2);
 
Leo Tien
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dirk, it print 'false';

My code like this:




Thanx for reply !!
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your Singleton is not threadsafe. Try synchronizing the getInstance method (add the synchronized modifier to the signature).
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you don't care about lazy initialization, then initializing the static instance while class loader loads the TaskListBean.

private static TaskListBean bean = new TaskListBean();
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read more about this problem in the following articles:

Double-checked locking: Clever, but Broken

Newsflash: Double-checked locking is still broken

Can double-checked locking be fixed?

Can ThreadLocal solve the double-checked locking problem?
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree that if possible remove the lazy initialization. Presumably the first reference to your class will be the getInstance() method and so lazy initialization isn't buying you much. If reloadData() takes a while to run then before the bean variable is assigned a value it is likely that another call to getInstance() will create a second object. Syncronizing the method would eliminate that possibility.

However, if you get rid of lazy initialization you might not need to synchronize the method as all static variables would be initialized before you could call getInstance().
 
Leo Tien
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, everyone:
First, thanx for reply.
I change my code make getInstance() synchronized, but the problem hasn't been solved, the two object is still different.But when I execute getInstance() in one method of TaskOAL and not init it by static, this problem is solved.
I run the code in web envieronment, Windows 2000 server + WebSphere + DB2.I create some thread to init data when my web project run, in WebSphere I meet some other problem, such as my owen thread cann't use JNDI reference access database. I don't confirm whether this Singleton problem raised by WebSphere ???

Thanx.
 
reply
    Bookmark Topic Watch Topic
  • New Topic