Difficulty: Easy
Correct Answer: By extending the Thread class or by implementing the Runnable interface
Explanation:
Introduction / Context:
Multithreading is a key feature in Java that allows programs to perform multiple tasks concurrently. Interviewers commonly ask how to create threads, because this knowledge is the starting point for understanding concurrency, synchronization, and performance. Java provides two primary approaches to define the code that a thread will execute.
Given Data / Assumptions:
Concept / Approach:
The two classic ways to create a thread in Java are: extending the Thread class and implementing the Runnable interface. In both cases, you provide a run() method that contains the code to be executed in the new thread. After constructing a Thread object, you start the thread by calling its start() method. Internally, the JVM creates a new call stack and invokes the run() method on that stack.
Step-by-Step Solution:
1. First approach: create a class that extends Thread and override the public void run() method with the desired code.
2. Create an instance of your subclass, for example MyThread t = new MyThread(), and then call t.start() to begin execution in a new thread.
3. Second approach: create a class that implements the Runnable interface and implement the public void run() method.
4. Create a Thread object by passing an instance of your Runnable to the Thread constructor, for example new Thread(new MyRunnable()).start().
5. In modern code, you often use anonymous classes or lambda expressions to implement Runnable inline, keeping the code short and focused.
Verification / Alternative check:
You can verify that a new thread is created by printing the current thread name inside the run() method using Thread.currentThread().getName(). When using either approach, you will see that the code is running in a different thread from the main thread. Remember that calling run() directly does not create a new thread; you must call start() to actually start concurrent execution.
Why Other Options Are Wrong:
Option B is incorrect because Object and Cloneable are not related to thread creation. Option C describes keywords related to synchronization and inheritance, not to creating threads. Option D is simply wrong; having multiple main methods does not create Java threads by itself. The recognized and standard mechanisms for thread creation are extending Thread and implementing Runnable.
Common Pitfalls:
A common mistake is to call run() directly instead of start(), which causes the code to execute in the current thread instead of a new one. Another pitfall is extending Thread when you also need to extend another class, which is not possible due to single inheritance. In those cases, implementing Runnable is usually preferred because it keeps your class hierarchies more flexible and separates the task from the thread that executes it.
Final Answer:
By extending the Thread class or by implementing the Runnable interface.
Discussion & Comments