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.
This exercise is part of the course
Optimizing Code in Java
Exercise instructions
- 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.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
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 = ____
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";
}
}