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
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
Error Handling
如果您需要运行不适用于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.