BaşlayınÜcretsiz Başlayın

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.

Bu egzersiz

Optimizing Code in Java

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

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

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

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";
    }
}
Kodu Düzenle ve Çalıştır