The goal is to be able to invoke execution of a separate thread from within the main class.
Some context: I have a program that must ru
To spawn a process that survives after the JVM terminates you'll need to use Runtime.exec()
. Since you want it to run after your main process ends, there are really two ways you can do it:
1) If you only want to process to run during a clean shutdown then you can do this:
public static void main(String[] args) {
doThisProgramsStuff();
spawnSecondProcess();
}
2) Or, if you want the process to run even with an unclean shutdown (or if it's a GUI application) then you can add a shutdown hook:
public static void main(String[] args) {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
spawnSecondProcess();
}
});
doThisProgramsStuff();
}
There is, however, a problem with all this. Once the JVM terminates the child process will have standard input/output attached to nothing, so it might fill it's output buffers and block. You'll need to do something to make sure this doesn't happen by either making sure the process does not produce output to those streams or by doing the Windows equivalent of redirecting to /dev/null.
I'm not altogether sure you can make a 'detached' thread in Java using the normal means of implementing Thread and kicking it off with run().
You might need to fork a thread using Runtime.exec()
, running java as wholly separate process, not as a thread.
If you mean: how can I start a Java thread that will not end when my JVM (java program) does?.
The answer is: you can't do that.
Because in Java, if the JVM exits, all threads are done. This is an example:
class MyRunnable implements Runnable {
public void run() {
while ( true ) {
doThisVeryImportantThing();
}
}
}
The above program can be started from your main thread by, for example, this code:
MyRunnable myRunnable = new MyRunnable();
Thread myThread = new Thread(myRunnable);
myThread.start();
This example program will never stop, unless something in doThisVeryImportantThing
will terminate that thread. You could run it as a daemon, as in this example:
MyRunnable myRunnable = new MyRunnable();
Thread myThread = new Thread(myRunnable);
myThread.setDaemon(true); // important, otherwise JVM does not exit at end of main()
myThread.start();
This will make sure, if the main() thread ends, it will also terminate myThread.
You can however start a different JVM from java, for that you might want to check out this question: Launch JVM process from a Java application use Runtime.exec?
Create a separate thread that executes your external program:
class MyRunner implements Runnable{
public void run(){
Runtime.exec("your cmd")
}
}
then start the thread in your main():
MyRunner myRunner = new MyRunner();
Thread myThread = new Thread(myRunner);
myThread.start();
This way your main program will continue running, while your background thread will start an external process and exit when this program exits.
Sounds like you want a script which calls the first program and then when the first finishes, it calls the second program.
Something like
program1
program2
EDIT: To run the two tasks in parallel you can do
program1 &
program2
in a bash/unit shell.
or in dos shell
start program1
program2