Junior — Middle
How to implement multithreading for processing a large volume of tasks, for example, 10,000 tasks?
sobes.tech AI
Answer from AI
To handle a large volume of tasks (for example, 10,000 tasks) efficiently in Java, it is recommended to use a thread pool from java.util.concurrent.
Main steps:
- Create an
ExecutorServicewith a fixed or dynamic thread pool size. - Submit tasks for execution using
submit()orexecute(). - Wait for all tasks to complete and properly shut down the pool.
Example:
import java.util.concurrent.*;
public class TaskProcessor {
public static void main(String[] args) throws InterruptedException {
int numberOfTasks = 10000;
int poolSize = Runtime.getRuntime().availableProcessors();
ExecutorService executor = Executors.newFixedThreadPool(poolSize);
for (int i = 0; i < numberOfTasks; i++) {
final int taskId = i;
executor.submit(() -> {
// Task processing
System.out.println("Processing task " + taskId);
// ...
});
}
executor.shutdown();
executor.awaitTermination(1, TimeUnit.HOURS);
}
}
This approach allows efficient utilization of CPU resources and manages the number of concurrently executing threads, avoiding the creation of too many threads, which can lead to decreased performance.