I know, using processbuilder we can create a process in java, but how do I create a thread inside a process? Also if i want to create multiple threads inside a process what is the best way of doing that?
Thanks in advance.
I know, using processbuilder we can create a process in java, but how do I create a thread inside a process? Also if i want to create multiple threads inside a process what is the best way of doing that?
Thanks in advance.
I'm looking for thread creation inside of new process.
Once launched, the launching application is not in control of the threads within the new process. Additional threads in the new process will be started as and when that processes code decides to. Only if you are the author of the code for the other process, would you be able to change how and when it spawns new threads.
The difference between processes and threads with respect to Java is that threads run within the same JVM instance, while processes run in different JVM instances.
For example, launching two instances of the same Java application, results in two processes, each running in their own JVM. Despite being the same application, they run independently of each other unless the application includes means of communicating between itself.
Thread creation in a different process would be the responsibility of that's process' Java code. If you are looking to create threads in one JVM under the direction of code in another JVM, you will have to implement an inter-process control mechanism (e.g. socket, control file, RMI, JMX, etc).
Without knowing your reason for spawning threads in a different process, I can only assume that you want some type of isolation. If it is data isolation you seek, consider revising your application's architecture to provide it intrinsically and follow one of the suggestions in Peter Lawrey's comment. A good starting point for ExecutorService is Java 8 Concurrency Tools: Threads and Executors.