Runnable vs Callable in Java: When to Use Each

🚀 Day 12 – Runnable vs Callable (and why Future matters) While working with threads, I explored the difference between "Runnable" and "Callable". 👉 We often use "Runnable": Runnable task = () -> { System.out.println("Running task"); }; ✔ Doesn’t return any result ✔ Cannot throw checked exceptions --- 👉 Now "Callable": Callable<Integer> task = () -> { return 10; }; ✔ Returns a result ✔ Can throw checked exceptions --- 💡 So where does "Future" come in? Future<Integer> result = executor.submit(task); Integer value = result.get(); 👉 "Future" acts as a placeholder for the result of an asynchronous computation --- 💡 Key takeaway: - Use "Runnable" → when no result is needed - Use "Callable" → when you need result or exception handling This becomes very useful when working with ExecutorService in real applications. #Java #BackendDevelopment #Multithreading #Concurrency #LearningInPublic

To view or add a comment, sign in

Explore content categories