aspose file tools*
The moose likes Threads and Synchronization and the fly likes Threads not dieing when program is stopped Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads not dieing when program is stopped" Watch "Threads not dieing when program is stopped" New topic
Author

Threads not dieing when program is stopped

Stuart Houston
Greenhorn

Joined: Aug 19, 2004
Posts: 6
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
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Threads not dieing when program is stopped