Reputation: 53
Is it possible to create a complete fork of a 'PROGRAM' in execution into two sub-programs from a single execution sequence ?
The sub-programs produced are completely identical. They have the same execution sequences and values but now they are two different programs. It is like creating a clone of an Object, thus giving us two different objects of the same type to work on. But instead of just an object and some values, here we want to create a completely parallel execution sequence of a Program already loaded in the JVM (would prefer an answer for Java).
Upvotes: 5
Views: 3673
Reputation: 10996
I'm not sure exactly what you're trying to do here. It sounds to me like you have a solution in mind to a problem that would best be solved in another way. Would something like this accomplish your end goal?
public class MyApp implements Runnable
{
public MyApp(int foo, String bar)
{
// Set stuff up...
}
@Override
public void run()
{
// Do stuff...
}
public static void main(String[] argv)
{
// Parse command line args...
Thread thread0 = new Thread(new MyApp(foo, bar));
Thread thread1 = new Thread(new MyApp(foo, bar));
thread0.start();
thread1.start();
}
}
Though I would probably put main()
in another object in a real app, since life-cycle management is a separate concern.
Upvotes: 4
Reputation: 83599
You seem to be looking for the Java equivalent of the fork system call from Unix.
That does not exist in Java, and it's unclear whether it would even be possible, as processes in Unix don't have a direct equivalent in the JVM (threads are less independent than processes).
There is however a fork framework planned for Java 7:
http://www.ibm.com/developerworks/java/library/j-jtp11137.html
It's not the same as Unix'es fork/join, but it shares some ideas and might be useful.
Of course you can do concurrent programming in Java, it's just not done via fork(), but using Threads.
Upvotes: 8
Reputation: 19321
Well, using ProcessBuilder you can spawn another program.
See Java equivalent of fork in Java task of Ant?
Upvotes: 3