• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads not dieing when program is stopped

 
Stuart Houston
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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?
 
Henry Wong
author
Marshal
Pie
Posts: 21420
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic