Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton Threads?

 
Murad Iqbal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Regards,
Murad.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
k.p thottam
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
bye
kpt
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic