How to scale threads according to CPU cores?
Asked Answered
S

6

112

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?

Sanhedrin answered 30/12, 2009 at 15:47 Comment(0)
R
123

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.

Roundtheclock answered 30/12, 2009 at 15:51 Comment(4)
This is basically correct, but be careful about performance on processors marketed with Intel's "hyper-threading". On a quad-core, this will return 8 instead of 4, but your performance may actually start dropping after 4 threads - so my own benchmarks tell me :)Helgeson
Hi, okay, didn't know, that this is possible. but when I split one task into several workunits and I need all part solution for the final workstep, how is this done? When I have several "yourThreads" how to I use join() for this, because I don't see, how these several threads are distinguishable? :) BTW: your link to Thread Pooling lead me to ibm.com/developerworks/library/j-jtp0730.html :)Sanhedrin
Look at the example here: java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/… It will tell you a more streamlined way to create and manage the thread pool... It may seem more complicated at first, but as with most things, it's more complicated because if it was simpler you'd just hit limitations sooner.Strung
Strange, I set my newFixedThreadPool with 1000 and it performs a lot better than availableProcessors. How does that work? How does it work on more threads than there are available CPU cores?Tallboy
C
67

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.

Cauley answered 30/12, 2009 at 16:3 Comment(4)
Hi DaveC, hmmm, haven't known that before, so I will have a look at this. And it can be scaled according to available cpu cores? Because I can't see that in you short examples. Best regards, AndreasSanhedrin
java.util.concurrent is highly scalableEtsukoetta
A fixed size pool with the number of available processors is often optimal for CPU bound processes. The first example here is all you need to do.Selig
As stated in the first comment of the accepted answer, it would be better to use half the number of reported "Processors", for two reasons: 1. if you have hyper-threading, the real number of processors is half of what is reported, and 2. it lets some processing power for the rest of the system to function (OS and other programs).Murrain
A
12

Option 1:

newWorkStealingPool from Executors

public static ExecutorService newWorkStealingPool()

Creates a work-stealing thread pool using all available processors as its target parallelism level.

With this API, you don't need to pass number of cores to ExecutorService.

Implementation of this API from grepcode

/**
     * Creates a work-stealing thread pool using all
     * {@link Runtime#availableProcessors available processors}
     * as its target parallelism level.
     * @return the newly created thread pool
     * @see #newWorkStealingPool(int)
     * @since 1.8
     */
    public static ExecutorService newWorkStealingPool() {
        return new ForkJoinPool
            (Runtime.getRuntime().availableProcessors(),
             ForkJoinPool.defaultForkJoinWorkerThreadFactory,
             null, true);
    }

Option 2:

newFixedThreadPool API from Executors or other newXXX constructors, which returns ExecutorService

public static ExecutorService newFixedThreadPool(int nThreads)

replace nThreads with Runtime.getRuntime().availableProcessors()

Option 3:

ThreadPoolExecutor

public ThreadPoolExecutor(int corePoolSize,
                      int maximumPoolSize,
                      long keepAliveTime,
                      TimeUnit unit,
                      BlockingQueue<Runnable> workQueue)

pass Runtime.getRuntime().availableProcessors() as parameter to maximumPoolSize.

Augustaugusta answered 19/4, 2016 at 15:48 Comment(0)
B
9

Doug Lea (author of the concurrent package) has this paper which may be relevant: http://gee.cs.oswego.edu/dl/papers/fj.pdf

The Fork Join framework has been added to Java SE 7. Below are few more references:

http://www.ibm.com/developerworks/java/library/j-jtp11137/index.html Article by Brian Goetz

http://www.oracle.com/technetwork/articles/java/fork-join-422606.html

Buttons answered 30/12, 2009 at 19:22 Comment(0)
W
7

The standard way is the Runtime.getRuntime().availableProcessors() method. On most standard CPUs you will have returned the optimal thread count (which is not the actual CPU core count) here. Therefore this is what you are looking for.

Example:

ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

Do NOT forget to shut down the executor service like this (or your program won't exit):

service.shutdown();

Here just a quick outline how to set up a future based MT code (offtopic, for illustration):

CompletionService<YourCallableImplementor> completionService = 
    new ExecutorCompletionService<YourCallableImplementor>(service);
    ArrayList<Future<YourCallableImplementor>> futures = new ArrayList<Future<YourCallableImplementor>>();
    for (String computeMe : elementsToCompute) {
        futures.add(completionService.submit(new YourCallableImplementor(computeMe)));
    }

Then you need to keep track on how many results you expect and retrieve them like this:

try {
  int received = 0;
  while (received < elementsToCompute.size()) {
     Future<YourCallableImplementor> resultFuture = completionService.take(); 
     YourCallableImplementor result = resultFuture.get();
     received++; 
  }
} finally {
  service.shutdown();
}
Wrenn answered 2/11, 2015 at 21:23 Comment(2)
shutdown call should be put in try finallyTem
@ChristopheRoussy you're very right, I've modified the snippet accordingly, thank you!Wrenn
J
4

On the Runtime class, there is a method called availableProcessors(). You can use that to figure out how many CPUs you have. Since your program is CPU bound, you would probably want to have (at most) one thread per available CPU.

Janycejanyte answered 30/12, 2009 at 15:51 Comment(2)
Hi Jason and Eric (I use one comment for both of your answers, because it's basicly the same). okay, that's nice to check, but this would be the first part. When I have the core count I have to have the threads as variable as this amount of cores. I tried this example before openbook.galileodesign.de/javainsel5/… (German!) and it uses a fixed thread. But I want to have the same programming using 2 core in a dual-core environment, and 4 cores in a quad-core environment. I don't want to change it manually. Is this possible? THX! :)Sanhedrin
@Andreas - See the updates I made to my post. I think that will help clarify the issue.Roundtheclock

© 2022 - 2024 — McMap. All rights reserved.