Junior — Middle
What is the result of the run() method in the Runnable interface?
sobes.tech AI
Answer from AI
The run() method in the Runnable interface in Java contains the code that should be executed in a separate thread. When an object implementing Runnable is passed to the Thread constructor and started with the start() method, the run() method of that object is called.
The run() method describes the logic of the task that will be executed concurrently with the main thread.
Example:
public class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("Thread is running");
}
}
public class Main {
public static void main(String[] args) {
Thread thread = new Thread(new MyRunnable());
thread.start(); // will call run() in a new thread
}
}
Thus, the run() method is the entry point for the code that should be executed in a separate thread.