🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
In Java, Runnable and Callable are two interfaces used for threading and task execution. Runnable has been part of Java since version 1.0 and represents a task that can be run by a thread. Callable was introduced in Java 5 and is similar to Runnable, but it can return a result and throw a checked exception.
2. Key Points
1. Runnable tasks do not return a result.
2. Callable tasks can return a result and throw checked exceptions.
3. Runnable has a run() method that does not return a value nor throw exceptions.
4. Callable has a call() method that returns a value and can throw an exception.
5. Runnable is typically used with threads, while Callable is used with ExecutorService, which can return Future objects.
3. Differences
| Runnable | Callable |
|---|---|
| Introduced in Java 1.0. | Introduced in Java 1.5 as part of the concurrency package (java.util.concurrent). |
| Represents a command that can be executed by a thread. | Represents a task that returns a result and may throw an exception. |
| The run() method does not return a value. | The call() method returns a value and can throw an exception. |
| The run() method cannot throw checked exceptions, only unchecked exceptions. | The call() method can throw checked exceptions. |
| Used with classes like Thread and interfaces like Executor. | Used with concurrency utilities like FutureTask and ExecutorService. |
| Ideal for fire-and-forget tasks that do not need to return a result or throw checked exceptions. | Suitable for tasks that need to return a result and might throw checked exceptions. |
4. Example
import java.util.concurrent.*;
public class RunnableVsCallable {
public static void main(String[] args) throws ExecutionException, InterruptedException {
// Using Runnable
Runnable runnableTask = () -> {
// Task implementation here
System.out.println("Running runnable task");
};
Thread runnableThread = new Thread(runnableTask);
runnableThread.start();
// Using Callable
Callable<String> callableTask = () -> {
// Task implementation here
return "Result from callable";
};
ExecutorService executorService = Executors.newFixedThreadPool(1);
Future<String> future = executorService.submit(callableTask);
// Print result of callable
System.out.println(future.get());
// Shutdown the executor service
executorService.shutdown();
}
}
Output:
Running runnable task Result from callable
Explanation:
1. The runnableTask implements Runnable and prints a message; it cannot return a result.
2. The callableTask implements Callable and returns a string; it can also throw exceptions.
3. future.get() is used to retrieve the result from the callableTask.
4. The executor service needs to be shut down explicitly after its use.
5. When to use?
Use Runnable when you want to run code in a thread without returning a result or throwing a checked exception.
- Use Callable when you need to return a result from your task or when your task might throw a checked exception and you want to handle it.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment