Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Singleton thread

Thread view: 
dushkin - 05 Jun 2007 15:31 GMT
Hi,
I would like to run a singleton class as a thread.
The following statement fails to compile of course, because the class
ctor is private as the pattern requests...

     new Thread(new MySingletonClass()).start();

Any help will be valuable!
Thanks.
Alex Hunsley - 05 Jun 2007 15:42 GMT
> Hi,
> I would like to run a singleton class as a thread.
[quoted text clipped - 4 lines]
>
> Any help will be valuable!

What you're trying to do is a bit odd.
Singletons are classically made to ensure only one instance exists,
right? So code can call getInstance() and not care.
And so the singleton class itself usually does any setup required for
its inner state.

If in your example you had:

 MySingletonClass.getInstance().start();

it wouldn't really make much sense, because if another bit of code had
already called this, you'd get IllegalThreadStateException (because
thread has already started).

The classic thing for a singleton to do is to do any setup required in
getInstance, thus hiding such setup code from the caller of getInstance.
I can't comment much further without knowing what it is you're trying to
achieve here, but why not have the singleton's getInstance method do any
thread stuff that is required?

lex


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.