Difference between Callable and Runnable and Future?

In Java, Callable, Runnable, and Future are three related interfaces that are used to perform concurrent programming and retrieve results. Here are the key differences between them:

Callable Interface

The Callable interface is a functional interface that represents a task that returns a result and may throw an exception. 
It has a single method: V call() throws Exception;
Here, V is the type of result returned by the task. The call() method must be implemented by the class that implements the Callable interface.

Runnable Interface

The Runnable interface is a functional interface that represents a task that does not return a result and may throw an exception.
It has a single method: void run();
Here, the run() method must be implemented by the class that implements the Runnable interface.

Future Interface

The Future interface represents the result of an asynchronous computation. It provides methods to check whether the computation is complete, retrieve the result of the computation, or cancel the computation. The Future interface has the following methods:

boolean isDone();
boolean isCancelled();
boolean cancel(boolean mayInterruptIfRunning) throws UnsupportedOperationException;
V get() throws InterruptedException, ExecutionException;
V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;

Here, V is the type of result returned by the computation. The get() method blocks until the computation is complete and returns the result. If the computation throws an exception, the get() method throws an ExecutionException.

Differences

The main differences between Callable, Runnable, and Future are:

Callable represents a task that returns a result and may throw an exception, while Runnable represents a task that does not return a result and may throw an exception.Callable has a single method call(), while Runnable has a single method run().Future represents the result of an asynchronous computation, while Callable and Runnable represent the task to be executed.Future provides methods to check whether the computation is complete, retrieve the result of the computation, or cancel the computation, while Callable and Runnable do not.x

Here is an example program that demonstrates the use of Callable, Runnable, and Future:

import java.util.concurrent.*; 

class MyCallable implements Callable<Integer> { 

private int num; 

public MyCallable(int num) { 

this.num = num; 

}

public Integer call() throws Exception { 

int sum = 0; f

or (int i = 1; i <= num; i++) { 

sum += i; 

} 

return sum; 

} } 


class MyRunnable implements Runnable { 

public void run() { 

System.out.println("Hello from MyRunnable!"); 

} } 


public class FutureExample { 

public static void main(String[] args) throws InterruptedException, ExecutionException { ExecutorService executor = Executors.newFixedThreadPool(2); 

// Submit a Callable task to the ExecutorService Future<Integer> future1 = executor.submit(new MyCallable(10)); 

// Submit a Runnable task to the ExecutorService Future<?> future2 = executor.submit(new MyRunnable()); /

/ Wait for the tasks to complete 

Integer result1 = future1.get(); future2.get(); 

System.out.println("Result1: " + result1); executor.shutdown(); }

 }


This program creates an ExecutorService with two threads and submits a Callable task and a Runnable task to it. The Callable task calculates the sum

A table that summarizes the differences between Callable, Runnable, and Future in Java:

InterfacePurposeMethodReturn TypeException
CallableRepresents a task that returns a result and may throw an exceptioncall()VException
RunnableRepresents a task that does not return a result and may throw an exceptionrun()voidException
FutureRepresents the result of an asynchronous computationget()VInterruptedException, ExecutionException, TimeoutException

Here, V represents the type of result returned by the computation.

Comments

Popular posts from this blog

Why Pointers are Eliminated in Java?

What is the advancement made in Hashmap in Java 8?

Integer.parseInt(“text”)