Why OS processes look faster than threads in java

I developed a multi-threaded Java application that uses a thread to process a batch of work. I have a main thread that sends work on different topics. Everything works great.

Now I have changed the application so that it runs in several java processes, each of which still performs multi-threaded processing. For example, I use 8 threads to process 8 jobs in one java process, now I have 2 Java processes, each of which has 4 threads that still process 8 jobs.

The application is modeled as follows: the dispatcher will do the work that needs to be done, and then send it to the thread pool. There is no synchronization or communication between threads.

I noticed a pretty big performance boost in a later approach, and I wonder why. Can anyone shed some light on this? Is an OS process more efficient than Java threads? Should I use this as a general rule when I really need this performance? thanks.

+4
source share
1 answer

It really depends on many factors. First, did you consider processor and memory load levels in both cases? I expect that in two processes the system will work with a greater load, so it will work faster.

Another reason for this may be the fact that you simply have two different heaps of memory that are cleared by two separate garbage collectors.

Also, how do processes interact with each other (file, pipe, queue, socket, etc.)? In the case of multiple processes, you cannot exchange memory (unless you make some system calls to the operating system that are expensive). Did you take this communication time into account when measuring time?

+2
source

Source: https://habr.com/ru/post/1486486/


All Articles