Implementing a thread pool for batch processing
Imagine you're developing a document processing system that needs to handle multiple documents simultaneously. Each document requires intensive processing that should be done in parallel, but you want to limit the number of concurrent operations to prevent system overload. You'll implement a solution using ExecutorService to efficiently manage the processing workload.
Este ejercicio forma parte del curso
Optimizing Code in Java
Instrucciones del ejercicio
- Create a fixed thread pool, containing 3 threads.
- Submit the
processDocument(doc)method as a task to the executor. - Shutdown the executor without forcing termination immediately.
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
public class DocumentProcessor {
public static void main(String[] args) throws InterruptedException {
List documents = List.of("Doc1", "Doc2", "Doc3", "Doc4", "Doc5");
List> futures = new ArrayList<>();
// Create a fixed thread pool with 3 threads
ExecutorService executor = Executors.____(____);
for (String doc : documents) {
// Submit the processDocument() method to the executor
futures.add(executor.submit(() -> ____(____)));
}
// Shutdown the executor and wait for termination
executor.____();
executor.awaitTermination(10, TimeUnit.SECONDS);
try {
for (Future future : futures) {
System.out.println(future.get());
}
} catch (ExecutionException e) {
System.out.println("Error processing documents: " + e.getMessage());
}
}
private static String processDocument(String docId) throws InterruptedException {
System.out.println("Processing " + docId + " on thread " + Thread.currentThread().getName());
Thread.sleep((long) (Math.random() * 1000));
return docId + " Processed";
}
}