Skip to content

12.3 Thread Pools & ExecutorService

import java.util.concurrent.*;

public class ThreadPoolDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 3 वर्कर थ्रेड्स का पूल बनाएं
        ExecutorService executor = Executors.newFixedThreadPool(3);

        // Callable से वैल्यू वापस ली जा सकती है
        Callable<String> downloadTask = () -> {
            Thread.sleep(1000);
            return "फाइल डाउनलोड पूर्ण!";
        };

        Future<String> futureResult = executor.submit(downloadTask);

        System.out.println("अन्य काम चल रहा है...");
        String result = futureResult.get(); // जब तक रिजल्ट न आए तब तक ब्लॉक रहेगा
        System.out.println("परिणाम: " + result);

        executor.shutdown(); // पूल को सुरक्षित रूप से बंद करें
    }
}

🧭 Navigation

Last updated on