Java MCQ: Which method must be implemented by all threads in Java?
a) run()
b) start()
c) execute()
d) stop()
Answer:
a) run()
Explanation:
In Java, the run()
method is the entry point for executing code in a thread. Every thread in Java is associated with an instance of a class that implements the Runnable
interface, which defines a single method: run()
.
When a thread is started using the start()
method, the Java Virtual Machine (JVM) invokes the run()
method of that thread’s Runnable
object. The code inside the run()
method defines the task that the thread will perform.
Implementing the run()
method is crucial for multi-threading in Java, as it allows concurrent execution of code, improving performance and enabling parallel processing of tasks.
Reference links:
https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html