How to create a user-defined number of threads?

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

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

Here is the only thing I could think of. I know this is completely untrue, and you cannot name the stream this way, but I am new (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 is not sure what I'm saying, I am trying to create the number of threads that the computer has, for example, the number of cores, or, if it uses Intel HyperThreading, twice as many cores. You may have more threads than the system can do right away, but I'm trying to do the most efficient thing and divide the total number of calculations by the number of threads that the system can perform at one time. I do not know how to let the user determine the number of threads, and then create this number of threads (or let the program determine the number of threads that the system has, and then create this number).

+4
source share
7 answers

You can find out how many processors are available for the JVM as follows:

 Runtime.getRuntime().availableProcessors() 

The optimal number of threads for splitting a pure numerical computation is probably one per processor. If any of these threads have to block IO from time to time, it could be more.

+4
source
 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]); } 

}

+3
source

If you are going to perform many different tasks - instead of trying to break one large task into several small tasks that are performed in parallel, you can use Executor :

 Executor e = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); // run a single task e.execute(new Runnable() { public void run() { // do stuff here } }); 

As others have pointed out, the optimal thread pool size depends on various factors, such as the number of I / O operations, but the number of available cores should be reasonable by default.

+2
source

that I like to use the Executor ExecutorService = Executors.newFixedSizeThreadPool (int numberOfThreads);

and then execute executor.execute (myRunnable) your runnables :) In this approach, you are not responsible for creating the threads, and you have a guarantee that you are not creating unnecessary threads.

but how do you know how many threads you should use, what you need to explore.

+1
source

It seems to me that what you are looking for is also a strategy for mapping tasks to threads. For example, a cycle such as:

 for(int i = 0; i < LARGE_NUMBER; i++) { f(i); } 

can convert to parallel version as:

 class Task implements Runnable { private int i; public Task(int i) { this.i = i; } public void run() { f(i); } } 

then replace the initial cycle of the cycle with:

 ExecutorService exec = Executors.newCachedThreadPool(); for(int i = 0; i < LARGE_NUMBER; i++) { exec.submit(new Task(i)); } exec.awaitTermination(1000, TimeUnit.HOURS); // wait for all tasks to finish 

ExecutorService will make sure to use multiple threads that match the available processors, as well as process threads so that tasks are performed on demand.

+1
source

The number of threads is processor independent. If you use one processor, your program will switch from one thread to another.

So, there is nothing β€œwrong” in your method (of course, if you use it correctly).

0
source

I think you are asking about the maximum number of threads that the system can have?

As discussed here , there is no maximum number of threads that the system can have (since Java programs run inside the Java virtual machine, the limit that you will have is the Java virtual machine - the JVM). The limit on the number of resources that you can allocate to your threads.

If you ask about how many threads are currently running (which may have been started by some external program or inside the JVM), you can probably find out, but I think this will not be useful for your case, when the threads you started are in no way connected with other threads running on the system!

0
source

Source: https://habr.com/ru/post/1389924/


All Articles