• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads not dieing when program is stopped

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a program which starts 2 threads. Each of these threads then starts an external batch file which runs a executable program.

The problem is that when the original program is killed the 2 threads do not stop and the executables carry on running

how can i stop the executables from running once the original program has been stopped?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stuart Houston:
I have a program which starts 2 threads. Each of these threads then starts an external batch file which runs a executable program.

The problem is that when the original program is killed the 2 threads do not stop and the executables carry on running

how can i stop the executables from running once the original program has been stopped?



What you are saying does not make sense. If your original program is killed, then everything about it, including the JVM, exits. This includes any threads that were created to run in the JVM.

Now for the two external programs that were started. It really depends on your OS. For example, under unix, the ownership of the program will be passed to process 1, which will reap any exit conditions, when those program exits.

You have two choices. Have your program return information to your "killer" about the two programs, such as PID, so they too, can be killed. Or arrange somekind of indication for them to know that your program was killed, so they too, will exit.

Henry
[ November 05, 2004: Message edited by: Henry Wong ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what you mean by "is killed". Say some code on thread 1 of the main() method starts threads 2 and 3 and then thread 1 exits the main() method. Threads 2 and 3 can keep running. The Thread doc says:


The Java Virtual Machine continues to execute threads until either of the following occurs:

* The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.

* All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.


That bit about daemon is correct but confusing syntax. It means any non-daemon threads will keep running until they stop on their own. If you don't want a thread to keep running after other threads have stopped, make it a daemon thread.

I have no idea what a daemon thread will do with the external process you srated with exec. Let me know if daemon helps!
reply
    Bookmark Topic Watch Topic
  • New Topic