Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scale threads according to CPU cores?

I want to solve a mathematical problem with multiple threads in Java. my math problem can be separated into work units, that I want to have solved in several threads.

I don't want to have a fixed amount of threads working on it, but instead an amount of threads matching the amount of CPU cores. My problem is, that I couldn't find an easy tutorial in the internet for this. All I found are examples with fixed threads.

How can this be done? Can you provide examples?

like image 748
Andreas Hornig Avatar asked Dec 30 '09 15:12

Andreas Hornig


People also ask

How many threads should I use per core?

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.

Do CPU threads count as cores?

Cores is an actual hardware component whereas thread is a virtual component that manages the tasks. Cores use content switching while threads use multiple CPUs for operating numerous processes. Cores require only a signal process unit whereas threads require multiple processing units.

What does 4 cores and 8 threads mean?

A i7-2670QM processor has 4 cores. But it can run 8 threads in parallel. This means that it only has 4 processing units (Cores) but has support in hardware to run 8 threads in parallel.


2 Answers

You can determine the number of processes available to the Java Virtual Machine by using the static Runtime method, availableProcessors. Once you have determined the number of processors available, create that number of threads and split up your work accordingly.

Update: To further clarify, a Thread is just an Object in Java, so you can create it just like you would create any other object. So, let's say that you call the above method and find that it returns 2 processors. Awesome. Now, you can create a loop that generates a new Thread, and splits the work off for that thread, and fires off the thread. Here's some pseudocode to demonstrate what I mean:

int processors = Runtime.getRuntime().availableProcessors(); for(int i=0; i < processors; i++) {   Thread yourThread = new AThreadYouCreated();   // You may need to pass in parameters depending on what work you are doing and how you setup your thread.   yourThread.start(); } 

For more information on creating your own thread, head to this tutorial. Also, you may want to look at Thread Pooling for the creation of the threads.

like image 196
JasCav Avatar answered Sep 29 '22 02:09

JasCav


You probably want to look at the java.util.concurrent framework for this stuff too. Something like:

ExecutorService e = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); // Do work using something like either e.execute(new Runnable() {         public void run() {             // do one task         }     }); 

or

    Future<String> future = pool.submit(new Callable<String>() {         public String call() throws Exception {             return null;         }     });     future.get();  // Will block till result available 

This is a lot nicer than coping with your own thread pools etc.

like image 26
DaveC Avatar answered Sep 29 '22 03:09

DaveC