Junior — Middle
Can the Runnable interface be considered as an interface for executing tasks in Java?
sobes.tech AI
Answer from AI
Yes, the Runnable interface in Java can be considered as an interface for executing tasks. It defines a single method:
public interface Runnable {
void run();
}
This method contains the code of the task to be executed. Runnable is often used to describe work that can be launched in a separate thread.
Example of usage:
Runnable task = new Runnable() {
@Override
public void run() {
System.out.println("Executing task in a separate thread");
}
};
Thread thread = new Thread(task);
thread.start();
Thus, Runnable serves as an abstraction for a task that can be executed, especially in a multithreaded context.