Is there some way to implement multi-tasking in Java (like "fork()" on UNIX)?

Finlay McWalter

There isn't anything very portable, but (if you're willing to provide an implementation for each of your target platforms) you can either:

  • Have the host OS execute a new JVM (typically by calling Runtime.exec()) and pass it the name of the classfile you want to "fork" to.
  • Use JNI (from C code) to create a new JVM
Neither of these is as good as fork, as the new subprogram doesn't inherit any of the state of its parent.

If you can possibly help it, try to avoid the need for this functionality in the first place, by using a Java application framework like JES (Java Embedded Server). This limits a few of the things you might like to do (mostly security and classloader stuff) but allows multiple programs to coexist in the same JVM. This has the additional benefit that it's much more efficient than a true fork - you still have only one JVM instance and only one OS process, which is much lighter weight than creating multiple processes each running a JVM.

[See also What is the difference between multithreading and multitasking? ]