views:

476

answers:

9

Hi,

If I want to run several processes in the same jvm without synchronizing (I don't care about multi things running at the same time... I only want to avoid re-instanciating the jvm), what is the best solution ?

Starting one thread and joining to wait until it dies, and then creates another thread to do another task ?

A: 

The Runtime.exec methods create a native process and return an instance of a subclass of Process that can be used to control the process and obtain information about it. The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.

Amir Afghani
hum, if i use ProcessBuilder or something like that, am I not creating another jvm ?
LB
yes you are... I would recommend against it but if it is other Java applications you want to run, why don't you just create a new thread and call the Application's main() from there?
Fredrik
You didn't specify what the other processes are. If they are Java programs, then yes of course you are creating new JVM instances. Do you have a good reason for worrying about that?
Amir Afghani
because I would have to synchronize some things right ? If I want to run several times the same java program with different arguments...
LB
Do those processes share memory, or data of some kind?
Amir Afghani
I would like to run sequentially the same main() but with different arguments, so they may share memory but I don't want too... I would to make the "jobs" as independant as possible without creating a new jvm and without all the Thread related stuff...Evertyhing should be local to the thread and disappear when the thread dies...
LB
A: 

A process runs a JVM; JVMs do not run processes (although they can start processes, as mentioned earlier). So there's no way to have multiple processes under the same JVM.

jdigital
so the only way to do it is by creating one thread for each job...
LB
+1  A: 

Your best standard solution is to run/deploy the apps in an application server. This is assuming that you are dealing with an app for which the overhead of the application server does not overshadow the advantage of having the single instance running. The downside is that deploy time is going to be considerable.

If you are simply looking to have small java apps that start up more quickly, you're mostly out of luck. There have been a few attempts to implement this in experimental JVMs. The idea being that if a JVM is running, then attach the class invocation to this running instance instead of staring up a new JVM. But none of this, as far as I can recall, ever made it into any of the mainstream JVMs.

Tim
A: 

Starting one thread and joining to wait until it dies, as you suggest, will just run your jobs serially. I'm assuming you just want to run multiple things concurrently. Just start up separate threads, either by creating new threads or Runnables. You just write a run() method that declares what you want your thread to to and call start():

public class T1 extends Thread {
   public void run () { .. do something useful .. }
}

public class T2 extends Thread { ... yadda yadda }

public static void main (String args[] )
{
 new T1().start();
 new T2().start();
}
Steve B.
no I actually want to run things sequentially... to avoid starting a new jvm each time...
LB
+2  A: 

You could use Ant's Java task to run multiple Java apps in the same JVM - just ensure that the fork parameter is set to false (the default). The linked help page provides some examples.

paulcm
A: 

You could either A.) Add that class to your classpath and just import/run the application Or probably more along the lines of what you are looking for, you can use Classloaders/Reflection to load up a class and find its main(String[] args) method then invoke it.

Sandro
+1  A: 

Seems to me the problem is that by default, you are stuck with a single entry point (ie. the jar's Main-Class, or whatever you specify on the java command line) when you execute your java app, and then the jvm terminates. If you run your processes from a BeanShell command line, you could manually invoke as many commands as you wanted.

For instance:

java -cp bsh-2.0b4.jar;yourapp.jar bsh.Console

And then,

bsh % com.domain.prog.Mainclass.main( new String[] { } );
Trevor Harrison
+3  A: 

Reminds me of JSR 121 Isolates. That spec completed but I'm not sure what, if anything, ever happened implementation-wise with this stuff. There is a followup JSR 284 as well.

Alex Miller
exactly what i'm looking for...
LB
A: 

You do not even need threads to run a number of other Java programs sequentially:

public class RunPrograms {

   public void main(args) {
      <for each applicationClass and args> {
        try {
           applicationClass.main(args)
        } catch Exception e {
           // if you want the entire set to die if one dies, rethrow (or don't catch)
           // otherwise somehow log the error and continue
        }
      }
   }
}

You will run into trouble if any of the programs calls System.exit(), but that will be true for any system using a single jvm.

Kathy Van Stone