In Java, there are two main interfaces that are used to define tasks that can be executed concurrently — Runnable and Callable. Both of these interfaces serve as essential tools for managing threads in Java, but they have distinct characteristics and use cases. Understanding the differences between these two interfaces is important for writing efficient multithreaded applications in Java.
If you don’t have a medium membership, you can use this link to reach the article without a paywall.
Runnable
The Runnable interface is part of the java.lang
package and represents a task that can be executed concurrently by a thread. It defines a single abstract method, run()
, which contains the code that will be executed when the thread is started.
@FunctionalInterface
public interface Runnable {
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general…