Runnable vs. Callable Interface in Java


In Java, both Runnable and Callable are interfaces used to define tasks that can be executed by threads or executors. They serve similar purposes but have key differences in their design and functionality.

1. Runnable Interface

  • Definition: The Runnable interface is a functional interface that represents a task that can be run by a thread. It does not return a result and cannot throw a checked exception.
  • Method: It has a single method:
    void run();
  • Usage: It is commonly used when you want to execute a task in a thread without needing a return value.
  • Example:
    Test.java
    package org.websparrow;
    
    
    public class Test {
    
        public static void main(String[] args) {
            Thread thread = new Thread(new MyRunnable());
            thread.start();
        }
    }
    
    class MyRunnable implements Runnable {
        
        @Override
        public void run() {
            System.out.println("Task is running");
        }
    }

2. Callable Interface

  • Definition: The Callable interface is also a functional interface that represents a task that can be run by a thread or executor. It returns a result and can throw a checked exception.
  • Method: It has a single method:
    V call() throws Exception;
  • Usage: It is used when you need the task to return a result or throw an exception.
  • Example:
    Test.java
    package org.websparrow;
    
    
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;
    
    
    public class Test {
        
        public static void main(String[] args) {
            ExecutorService executor = Executors.newSingleThreadExecutor();
            Callable<String> callable = new MyCallable();
            Future<String> future = executor.submit(callable);
    
            try {
                String result = future.get(); // Blocks until the result is available
                System.out.println(result);
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            } finally {
                executor.shutdown();
            }
        }
    }
    
    class MyCallable implements Callable<String> {
        @Override
        public String call() throws Exception {
            return "Task completed";
        }
    }

3. Key Differences

  1. Return Type:
    • Runnable: Does not return a result (void return type).
    • Callable: Returns a result (V return type).
  2. Exception Handling:
    • Runnable: Cannot throw checked exceptions.
    • Callable: Can throw checked exceptions.
  3. Usage Context:
    • Runnable: Suitable for tasks that do not require a result or exception handling.
    • Callable: Suitable for tasks that require a result or need to handle exceptions.

4. Summary

  • Use Runnable when you have a task that does not need to return a result and does not need to throw checked exceptions.
  • Use Callable when you have a task that needs to return a result and/or may throw checked exceptions.

References

  1. Functional Interface in Java 8
  2. How to Create Thread in Java
  3. Interface Runnable
  4. Interface Callable<V>

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.