Java Callable Interface Explained with Code Samples
In this article, I will be explaining the Callable interface in Java. I will be explaining what is Runnable and Callable in Java and what is the use of the Callable interface. Runnable Interface Right from the start, the Runnable interface has been associated with multi-threaded programming. The Runnable interface provides a run method that executes code in a separate thread. The following code demonstrates this: public class RunnableDemo { public static void main(String[] args) { System.out.println("Creating new thread in main"); Runnable r = () -> System.out.println("Executing thread body...."); Thread myThread = new Thread(r); myThread.start(); System.out.println("Completed main"); }} So this code first creates a new Runnable interface and uses a lambda expression to implement the run method. The code then creates a new Thread and invokes the start method which spawns a new Thread and executes the code in the Runnable implementatio...