ComeçarComece de graça

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 exercício faz parte do curso

Optimizing Code in Java

Ver curso

Instruções do exercício

  • 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.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

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";
    }
}
Editar e executar o código