Runnable vs Callable interfaces in Java

The Runnable interface is the most widely used interface in Java to provide multithreading features, to execute tasks parallelly.

Runnable interface:public interface Runnable
void run()
package com.techpredators.threads;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class RunnableExample {
public static void main(String args[]) {
Runnable runnable_1 = () -> {
System.out.println("Executing Thread_1 inside");
};
Runnable runnable_2 = () -> {
System.out.println("Executing Thread_2 inside");
};
Runnable runnable_3 = () -> {
System.out.println("Executing Thread_3 inside");
};
ExecutorService executorService = Executors.newFixedThreadPool(5);executorService.submit(runnable_1);
executorService.submit(runnable_2);
executorService.submit(runnable_3);
executorService.shutdown();
}
}

Callable Interface:

The Callable interface is the improvised version of the Runnable interface, this interface is introduced since Java 1.5

Callable Interface:
Public interface Callable
V call() throws Exception
package com.techpredators.threads; import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import package com.techpredators.threads;
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;
import java.util.stream.Stream;
public class CallableExample {public static void main(String[] args) throws Exception {try {
ExecutorService service = Executors.newSingleThreadExecutor();
Stream<String> stream = Stream.of(“abc”, “def”, “ghi”, “jkl”);
SampleTask sumTask = new SampleTask(stream);
Future<Long> future = service.submit(sumTask);
Long result = future.get();
System.out.println(result);
}
catch (InterruptedException interruptedException)
{
System.out.println(“InterruptedException occured “ + interruptedException.getMessage());
}
catch (ExecutionException executionException)
{
System.out.println(“ExecutionException occured “ + executionException.getMessage());
}
}
}
class SampleTask implements Callable<Long> {private Stream<String> stream = null;
public SampleTask(Stream<String> stream)
{
this.stream = stream;
}
@Override
public Long call() throws Exception
{
return this.stream.count();
}
}