• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to implement start/stop/restart in daemon thread

 
Vivek Jain
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've written a continuesly running thread program in Java using runnable implenetation approach. Now, since its a continuously running program, I've a while(true) loop in run method. Now, my question is how do I halt this program if I want to stop running it. I do not want to use "kill" command. I want something similar to a scirpt type command like "daemon stop", "daemon start" or "daemon restart".

Can anyone tell me how to implement this?

Thanks!
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have set this thread up as a daemon, surely it will stop as soon as it is no longer required. Don't try stop pause or resume methods; they don't work.
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Threads.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
instead of while(true), consider having a boolean variable call it isRun or whatever. e.g. while(isRun) The loop will execute as long as isRun is true, but as soon as some other part of the program sets it to false, the loop stops. That's one way. I'm not sure if it applies to your situation or not.

the stop() command for threads that I know about is deprecated.
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vivek,

I have written a sample code based on your requirement.




Swing UI



sat
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!