Interview Q&A · Java · 13 questions in this topic
Creating Threads: Runnable vs Thread — Java Interview Q&A
How to create threads with Runnable and Thread, how the scheduler runs them, and key lifecycle rules. Part of Threads & Concurrency · 294 questions in the Java bank.
Which Java APIs support threads?
Answer
The foundation is the Thread class and the Runnable interface in the java.lang package, plus the wait and notify methods on Object for coordination. The java.util.concurrent package adds the modern toolkit, including ExecutorService, Future, locks, concurrent collections, and atomic variables. Together these cover raw thread control and higher level task execution. Interviews expect you to name both the classic APIs and the concurrent package.
Key points
- 1java.lang.Thread and java.lang.Runnable for basic thread creation.
- 2Object wait, notify, and notifyAll for low level coordination.
- 3java.util.concurrent executors, futures, locks, and concurrent collections for modern code.
- 4Callable, Future, and CompletableFuture for tasks that return results.
What is the main thread?
Answer
The main thread is the first thread the JVM starts when an application launches, and it runs the main method. It can create child threads, which in turn can create further threads. It is a non-daemon thread, so the JVM keeps running until it and all other non-daemon threads finish. When only daemon threads remain, the JVM is allowed to exit.
How many ways can threads be created?
Answer
The two classic ways are extending the Thread class and implementing the Runnable interface. Modern Java adds more options, such as implementing Callable and submitting it to an ExecutorService, or passing a lambda to a thread or executor. Frameworks also provide thread factories and virtual threads in Java 21 and later. In practice, implementing Runnable or submitting a task to an executor is preferred over subclassing Thread.
Key points
- 1Extend Thread and override run, then call start.
- 2Implement Runnable and pass it to a new Thread, then call start.
- 3Implement Callable and submit it to an ExecutorService for a result.
- 4Pass a lambda or method reference to a Thread or an executor, including virtual threads with Thread.ofVirtual.
Create a thread by implementing Runnable.
Answer
You define a class that implements Runnable and places the task logic in its public void run method. You then pass an instance of that class to the Thread constructor and call start to launch a new thread. Calling start creates a new call stack and eventually invokes run on that new thread. You must call start, not run directly, because calling run would execute on the current thread.
Example
public class MyTask implements Runnable { public void run() { System.out.println("Running in " + Thread.currentThread().getName()); } } Thread t = new Thread(new MyTask()); t.start();
Create a thread by extending Thread.
Answer
You define a subclass of Thread and override its public void run method with the task logic. You then create an instance of the subclass and call start to launch a new thread. As with Runnable, start creates a new thread of execution that calls run, while calling run directly does not. This approach works but consumes your one chance at inheritance, which is why Runnable is usually preferred.
Example
public class MyThread extends Thread { public void run() { System.out.println("Running in " + Thread.currentThread().getName()); } } MyThread t = new MyThread(); t.start();
Which approach is best?
Answer
Implementing Runnable is generally preferred over extending Thread. It leaves the inheritance hierarchy free, since Java allows only single inheritance of classes. It also separates the task from the thread that runs it, so the same task can be run by a plain thread, a pool thread, or a virtual thread. That flexibility makes Runnable work naturally with executors, while extending Thread ties the task to one thread object.
💡 Interview tip: say separating task from thread lets the same Runnable run on pools and virtual threads.
What does the thread scheduler do?
Answer
The thread scheduler decides which runnable thread gets CPU time at any moment. In Java the scheduler is implemented by the JVM together with the host operating system. It considers factors such as thread state and priority, but priorities are only hints and behaviour varies by platform. Application code must therefore never depend on a particular scheduling order for correctness.
What is the thread lifecycle?
Answer
A Java thread moves through the states defined in the Thread.State enum. It starts in NEW before start is called, then becomes RUNNABLE when it can run, including time actually running on a CPU. It can move to BLOCKED while waiting for a monitor lock, to WAITING after calls such as join without timeout or Object wait, or to TIMED_WAITING after sleep or a timed wait, and it ends in TERMINATED after run completes. The older textbook list in the source is outdated, so use these six enum names in interviews.
Key points
- 1NEW: created but start has not been called yet.
- 2RUNNABLE: ready to run or running on a CPU.
- 3BLOCKED: waiting to enter a synchronized block or method.
- 4WAITING: parked until another thread notifies or joins, with no timeout.
- 5TIMED_WAITING: parked with a timeout, for example in sleep or timed join.
- 6TERMINATED: run has finished and the thread cannot be restarted.
💡 Interview tip: list the six Thread.State names exactly, since older books use different names.
Can a dead thread be restarted?
Answer
No, a thread that has reached the TERMINATED state cannot be restarted. Calling start a second time throws IllegalThreadStateException. If the work must run again, you must create a new Thread object with the same task. This rule applies equally to threads created by subclassing Thread and to threads wrapping a Runnable.
Can one thread block another thread?
Answer
One thread cannot forcibly pause or stop another thread in modern Java. A thread can only block itself by calling operations such as sleep, wait, or join, or it can politely request action from another thread with interrupt. The old suspend and stop methods that could freeze or kill another thread are deprecated because they were unsafe and could leave locks held. Correct interview answer: no forced blocking, only cooperative coordination.
Can an already-started thread be started again?
Answer
No, start may be called only once per Thread object. A second call throws IllegalThreadStateException, even if the thread already finished. This is the same rule that prevents restarting a dead thread. To run the task again, construct a fresh Thread instance.
What happens if run() is not overridden?
Answer
If a Thread subclass does not override run and was not given a Runnable target, its run method does nothing and the thread simply ends. If the Thread was constructed with a Runnable target, the default run delegates to that target run method. So forgetting to override run is harmless but usually a bug, since the new thread starts and immediately terminates. Modern executors avoid this confusion by taking the task as an explicit argument.
Can run() be overloaded?
Answer
Yes, you can declare additional run methods with parameters, since run follows normal overloading rules. However, the thread machinery always calls the no-argument run method when the thread starts. Overloaded versions never run automatically and can only be invoked like ordinary methods. If you need parameters, pass them through fields, a constructor, or a lambda capture instead.
Example
public class MyThread extends Thread { public void run() { System.out.println("no-arg run called by start"); } public void run(int n) { System.out.println("overload never called by start: " + n); } } MyThread t = new MyThread(); t.start();
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission