Does it make sense to have more than two worker threads on a machine with two cores?

Imagine that I have a set of inputs (for example, Queue<ISomeInputDataItem>). I have ExecutorServiceone in which there are several workflows that take input elements from this collection, process them and put the results in the output collection. The order of the elements in the input and output collection does not matter.

If I run this code on a machine with X-cores, is the number of worker threads in the thread pool correct should be X, because

  • if it is less than X, then some kernels will not be used during input processing and
  • the number of threads involved in the calculation is limited by the number of cores (if I have 2 worker threads and 1 core, they will share the time of this core).

?

Note. I am only interested in Java thread pools in the above conditions (not in all possible general cases).

+4
source share
3 answers

If the threads are connected to the CPU, that is, the code never expects anything and the maximum processing speed of ticks, then the use of X is optimal. Although using up to 2X was just fine in my tests on Java 6. (Even 4X was just a slight slowdown, but YMMV)

However, if streams wait a lot of time on disk or network IO, synchronized data structures, GPUs, etc., then you should use more than X, so it will almost always work, even if several are waiting.

+4
source

Adding to what @ user949300 said,

, . , " " . , .

+1

X-, X,

X, , , ( 2 1 , )


This is absolutely correct. In addition, even if you have 2 cores and 2 workflows, if the underlying data structure that they share for the queue is an ArrayList or, in fact, some adjacent block, they end up with the same performance lag.

  • This is due to the fact that the data structure is loaded into memory in the form of pages and in the case of a continuous block, as soon as the queue is blocked by one thread (by loading the page into memory), another thread must wait until it is released

0
source

All Articles