• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

How to implement start/stop/restart in daemon thread  RSS feed

 
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!
 
Marshal
Posts: 64494
225
  • 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.
 
Sheriff
Posts: 21744
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Threads.
 
Ranch Hand
Posts: 686
  • 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.
 
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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!