Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to regard Hyper-Threading when deciding thread pool size?

I've read several Questions and articles regarding how to decide on a size for a thread pool. Questions include:

  • How to choose thread pool size?
  • What is the relationship between number of CPU cores and number of threads in an app in java?
  • Java thread pool size (Executors)

Articles include:

  • Tuning the Size of Your Thread Pool by Kirk Pepperdine

However, none of these directly address the issue of Hyper-Threading on Intel chips.

On a computer with Hyper-Threading enabled, should I consider the virtual cores when deciding thread pool size?

Take for example Brian Goetz’ advice in the Java Concurrency In Practice book that, generally speaking, for a CPU-bound app one might consider using ( # of cores + 1 ) as the number of threads. For an Intel Core i7 chip with 4 real cores and 8 virtual (Hyper-Threading) cores, would that formula be ( 4 + 1 ) or ( 8 + 1 )?

Also, how much of difference does the nature of the app matter in how to consider the Hyper-Threading cores?

Contrary to the mention above, my own app is not CPU bound. Instead my app is a server-side Vaadin app where the threads are making Internet connections and hitting a local database through JDBC, several times a minute. Given that Hyper-Threading is basically a second set of registers attached to the same core, perhaps a CPU-bound app should consider only real cores while a network/IO-bound app should consider virtual cores?

Lastly, does the type of Intel chip affect Hyper-Threading and therefore the calculation of thread pool size? Specifically, is there a difference regarding this issue between a Xeon and a Core i7/i5? For example, the difference between a current MacBook (Core i7) and Mac Pro (Xeon).

I realize there are many variables involved, and this is a complex topic. No perfectly precise answers are possible. I'm just looking for some general rules and advice to help out programmers like me who are not as savvy about such hardware matters.

like image 823
Basil Bourque Avatar asked May 15 '15 23:05

Basil Bourque


People also ask

How many threads should a thread pool have?

ThreadPool will create maximum of 10 threads to process 10 requests at a time. After process completion of any single Thread, ThreadPool will internally allocate the 11th request to this Thread and will keep on doing the same to all the remaining requests.

When we should use thread pool?

You would use threads for long-lasting processes. If you have a lot of small tasks, you can use a thread pool. The pool allocates threads only once and re-uses them to avoid unnecessary thread creation.


1 Answers

How to regard Hyper-Threading when deciding thread pool size?

The short answer is Don't.

The longer answer is that Goetz's "formula" is actually just a rule of thumb. The language used here

... "generally speaking, for a CPU-bound app one might consider using ( # of cores + 1 ) as the number of threads"

makes that clear. There are all sorts of reasons why that "rule of thumb" number might give you suboptimal performance.

The correct approach is:

  1. Pick a number
  2. Measure performance
  3. Adjust number and go to step 2.

.... until you arrive at a thread pool size that gives roughly the best answer for your use-case.

The other thing to note is that when you are building a server-based system is that performance is only one of many considerations. Another is how your system performs under extreme load. If you optimize your performance based on a "best case" workload without considering behaviour under overload situations, then you can be in for a nasty shock if something goes wrong.

Optimizing solely for maximum throughput can have bad consequences...

like image 153
Stephen C Avatar answered Oct 07 '22 07:10

Stephen C