Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to choose several processes over threads in Java?

For what reasons would one choose several processes over several threads to implement an application in Java?

I'm refactoring an older java application which is currently divided into several smaller applications (processes) running on the same multi-core machine, communicating which each other via sockets.

I personally think this should be done using threads rather than processes, but what arguments would defend the original design?

like image 778
wannabeartist Avatar asked Feb 18 '12 07:02

wannabeartist


People also ask

When would you use processes over threads?

When it comes to processes, the OS usually protects them from one another. Even if one of them corrupts its own memory space, other processes are not affected. Another benefit of using processes over threads is that they can run on different machines. On the other hand, threads normally have to run on the same machine.

Why are multiple threads usually preferred to multiple processes?

On a multiprocessor system, multiple threads can concurrently run on multiple CPUs. Therefore, multithreaded programs can run much faster than on a uniprocessor system. They can also be faster than a program using multiple processes, because threads require fewer resources and generate less overhead.

Why would you choose to use multiple threads versus separate processes in a server?

You'd prefer multiple threads over multiple processes for two reasons: Inter-thread communication (sharing data etc.) is significantly simpler to program than inter-process communication. Context switches between threads are faster than between processes.

Why are processes better than threads?

Processes are totally independent and don't share memory. A thread may share some memory with its peer threads. Communication between processes requires more time than between threads. Communication between threads requires less time than between processes .


1 Answers

I (and others, see attributions below) can think of a couple of reasons:

Historical Reasons

  • The design is from the days when only green threads were available and the original author/designer figured they wouldn't work for him.

Robustness and Fault Tolerance

  • You use components which are not thread safe, so you cannot parallelize withough resorting to multiple processes.

  • Some components are buggy and you don't want them to be able to affect more than one process. Say, if a component has a memory or resource leak which eventually could force a process restart, then only the process using the component is affected.

  • Correct multithreading is still hard to do. Depending on your design harder than multiprocessing. The later, however, is arguably also not too easy.

  • You can have a model where you have a watchdog process that can actively monitor (and eventually restart) crashed worker processes. This may also include suspend/resume of processes, which is not safe with threads (thanks to @Jayan for pointing out).

OS Resource Limits & Governance

  • If the process, using a single thread, is already using all of the available address space (e.g. for 32bit apps on Windows 2GB), you might need to distribute work amongst processes.

  • Limiting the use of resources (CPU, memory, etc.) is typically only possible on a per process basis (for example on Windows you could create "job" objects, which require a separate process).

Security Considerations

  • You can run different processes using different accounts (i.e. "users"), thus providing better isolation between them.

Compatibility Issues

  • Support multiple/different Java versions: Using differnt processes you can use different Java versions for your application parts (if required by 3rd party libraries).

Location Transparency

  • You could (potentially) distribute your application over multiple physical machines, thus further increasing scalability and/or robustness of the application (see @Qwe's answer for more Details / the original idea).
like image 157
Christian.K Avatar answered Nov 15 '22 17:11

Christian.K