File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Singleton Threads? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Singleton Threads?" Watch "Singleton Threads?" New topic

Singleton Threads?

Murad Iqbal
Ranch Hand

Joined: Dec 09, 2003
Posts: 90
Hi all, Please advise if there is a problem in creating a Singleton Thread. The only doubt I have in mind is what will happen if the instance of the Singleton Thread is in its run method and another Object gets access to the thread by singletonThread = SingletonThread.getInstance() call and then invokes its run method?
Please advise.
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
I don't understand why you would want to do that. Typically you don't call the run method of a thread directly (which wouldn't be a problem in general), but use the start method instead. And I am quite sure that you can't start a thread several times concurrently (a look into the JavaDoc should make that sure).
What do you want to accomplish???

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
k.p thottam
Ranch Hand

Joined: Jun 29, 2001
Posts: 77
Besides the issue of syntax , ( you start a thread via start() , not via run), the bigger issue that you must be aware of is that even after the thread is dead , the instance will be still alive until it is garbage collected , which means that the job that the thread is to do will not be taking place .
I just designed a singleton cleaner class that requires each morning to find documents that are expired and delete them , the reason it had to be singleton is because in addition to deleting the document , it will also have to remove a reference from the lucence index , which is an action that must be serialized .
In my design rather than making my thread a singleton , I made the invoking class a singleton , with a single reference to the thread class . that way when the thread is dead a single new thread is created , but the thread creation is in a single instance , so there is only one thread at a time .

K.P.Thottam (K.P.T)<br /> <br />Sun Certified Enterprise Architect,TOGAF 8 Certified,Certified Information System security Professional (CISSP),SCJDWS,SCWCD,SCJP,MCP
I agree. Here's the link:
subject: Singleton Threads?
It's not a secret anymore!