Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create a user-defined number of threads?

In the program I'm working on, I want the user to be able to input the number of processing threads that their processor has so that the program can divide the workload (it's doing general calculations) between the number of threads the user's computer has.

Alternatively, is there a way that you can get the program to detect the system configuration to get the number of threads without asking the user? This would be preferable, but I don't know if there is a way to do this.

Here is the only thing I've been able to think of. I know that it is completely incorrect, and that you can't name a thread that way, but I'm a beginner (still in high school), and I just wanted to include something to show that I'm trying.

for(int i = 0; i < threads; i++) {

    Thread thread(i) = new Thread(){
        public void run() {
            double endNum = 0;

            for(double numberRun = 0; numberRun <= calcs/threads; numberRun++){
                endNum += (num * 999999999);
            }

            System.out.println("Thread " + i + " complete! The numerical result of the calculation is " + endNum);
        }
    };
}

Everyone who isn't sure what I'm saying, I'm trying to create the number of threads that the computer has, aka the number of cores, or, if it's using Intel's HyperThreading, twice the number of cores. You can have more threads than the system is capable of executing at once, but I'm trying to do the most efficient thing and divide the total number of calculations into the number of threads that the system is capable of executing simultaneously. I do not know how to let the user define the number of threads and then create that number of threads (or let the program determine the number of threads the system has and then create that number).

like image 432
user1137371 Avatar asked Dec 21 '22 03:12

user1137371


2 Answers

You can find out how many processors are available to the JVM like this:

Runtime.getRuntime().availableProcessors()

The optimal number of threads for splitting a pure numeric calculation over is probably one per processor. If any of those threads have to occasionally block on IO, it might be more.

like image 69
Russell Zahniser Avatar answered Dec 24 '22 02:12

Russell Zahniser


class Task implements Runnable {
  Task(/** Whatever you need to identify the subset of tasks to run goes here*/) {

  }

  public void run() {
    // do the calcs
  }
}

int nProcs = Runtime.getRuntime().getAvailableProcessors();

Task[] tasks = new Task[nProcs];
for (int i = 0; i < nProcs; i++) {
   tasks[i] = new Task(/** Whatever parameters for this batch*/);
   new Thread(tasks[i]).start(); 
   // Alternatively, use Executors.newFixedThreadPool() to get a service above, and submit the runnable there, getting a future for the result, something like this:
   Future f = executor.submit(task[i]);
}

}

like image 26
James Avatar answered Dec 24 '22 02:12

James