Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threads per Processor

In Java, is there a programmatic way to find out how many concurrent threads are supported by a CPU?

Update

To clarify, I'm not trying to hammer the CPU with threads and I am aware of Runtime.getRuntime().availableProcessors() function, which provides me part of the information I'm looking for.

I want to find out if there's a way to automatically tune the size of thread pool so that:

  • if I'm running on a 1-year old server, I get 2 threads (1 thread per CPU x an arbitrary multiplier of 2)
  • if I switch to an Intel i7 quad core two years from now (which supports 2 threads per core), I get 16 threads (2 logical threads per CPU x 4 CPUs x the arbitrary multiplier of 2).
  • if, instead, I use a eight core Ultrasparc T2 server (which supports 8 threads per core), I get 128 threads (8 threads per CPU x 8 CPUs x the arbitrary multiplier of 2)
  • if I deploy the same software on a cluster of 30 different machines, potentially purchased at different years, I don't need to read the CPU specs and set configuration options for every single one of them.
like image 907
Leo Avatar asked Oct 18 '08 16:10

Leo


People also ask

How many threads should a processor have?

Each CPU core can have two threads. So a processor with two cores will have four threads. A processor with eight cores will have 16 threads. A processor with 24 cores (yes, those exist), will have 48 threads.

What are threads in a processor?

A thread is a virtual version of a CPU core. To create a thread, Intel CPUs uses hyper-threading, and AMD CPUs uses simultaneous multithreading, or SMT for short (they're the same thing). These are both names for the process of breaking up physical cores into virtual cores (threads) to increase performance.

How many threads can a 4 core CPU run?

Each CPU-core can run only one thread at any given moment. So for example in a quad-core machine, the maximum number of threads that can run in parallel is 4.

What does 4 cores 8 threads mean?

This means that it only has 4 processing units (Cores) but has support in hardware to run 8 threads in parallel. This means that a maximum of four jobs run in on the Cores, if one of the jobs stall due to for example memory access another thread can very fast start executing on the free Core with very little penalty.


1 Answers

Runtime.availableProcessors returns the number of logical processors (i.e. hardware threads) not physical cores. See CR 5048379.

like image 130
Tom Hawtin - tackline Avatar answered Oct 12 '22 18:10

Tom Hawtin - tackline