简体   繁体   中英

Why do we need multiprocess programming in Java?

Java supports multithread well, and Java also supports multiprocess via Process, ProcessBuilder and Runtime.exec()...

I know clearly the definitions of thread and process, and the differences between them in os concepts.

But I wonder why and in what situation do we need to use a multiprocess instead of multithread in Java application?

Don't necessarily think of processes as replacements for threads. Processes in java are convenient ways of executing external commands. They aren't really that useful in general parallelism scenarios, as they are cumbersome to start and synchronize.

Another good use of them is to isolate native code (or any other code that you cannot control) that may not terminate or cause stack overflows. If this were to be run inside a thread it could bring the entire process down. Instead, you can spawn a new process and then forcibly kill it without worrying to much about it.

On top of my head, reasons for using processes as a complement to threads could be

  • Robustness, one failing process won't affect another
  • Separation. Launching multiple JVM allows running the same classes without worrying about interference (eg, easier to use non-thread safe libraries)
  • Generally to be able to launch external commands (eg, non-java)
  • Thread affinity. In some OS's it might provide better cache semantics with processes shared over multiple CPU's rather than threads, especially when considering a thread-shared working set.

Still, in most applications threading is the preferred tool for memory reasons, easy to spawn and its (relatively) simple ease of use.

当同步不是问题时,您可能需要它,即不干扰相同数据的进程,但您需要同时收集这些进程的输出,这意味着您需要并行运行它们,尽管它们完全不同流程。

There is no run-away protection in the JVM.

If you have a thread which will not stop, the only way to forcefully stop it, is to have the operating system kill its JVM. By having separate processes you can keep the rest of the application running.

It depends, there is no simple answer. Depending on the OS/JVM, Threads and Processes could mean different things and could have different levels of isolation.

Why have ability to use both?? Here is an example

Consider a scenario when you have to use some legacy or thirdparty native (C++) library that is not thread safe (or offers no guarantees). I imagine if you have to streamline your server to utilize high number of processors. a multi-process architecture would be more suitable.

Isolation

  • If you try to use multi-threading, you risk running into synchronization, dead locks, active locks, etc... issues.
  • If you use multi-process architecture, you would guarantee some level of isolation in terms of memory access. You'd be able to

Error Handling

  • If one of those multi-process components fails, you are more likely able to recover and possibly compensate with another process, instead of bringing the entire JVM down in your starting process. Even though there are way to handle and protect against that, wrapping it in an isolated process will make your life easier. If you ever had to code defensively against some badly written ancient aliens native library? You'll know what I mean.

如果您需要运行不适用于JVM但可以使用o / s命令行运行的程序,则不能将其作为线程运行,而只能作为进程运行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM