Java Performance Processes vs Threads

前端 未结 3 1376
耶瑟儿~
耶瑟儿~ 2021-02-05 14:46

I am implementing a worker pool in Java.

This is essentially a whole load of objects which will pick up chunks of data, process the data and then store the result. Beca

3条回答
  •  醉酒成梦
    2021-02-05 15:29

    Well usually, when discussing multi processing (/w one thread per process) versus multi threading in the same process, while the theoretical overhead is bigger in the first case than in the latter (and thus multi processing is theoretically slower than multi threading), in reality on most modern OSs this is not such a big issue. However when discussing it in the Java context, starting a new process is a lot more costly then starting a new thread. Starting a new process means starting up a new instance of the JVM which is very costly especially in terms of memory. I recommend that you start multiple threads in the same JVM.

    Moreover, if you say inter-thread communication is not an issue, you can use Java's Executor Service to get a fixed thread pool of size 2x(number of available CPUs). The number of available CPU's can be autodetected at runtime via Java's Runtime class. This way you get a quick simple multithreading going without any boiler plate code.

提交回复
热议问题