Back to Predict the Output
Q6Question 6 of 10

What happens if you call start() twice on the same Thread?

Key Concepts
  • IllegalThreadStateException is thrown on the second start() call
  • A Thread can only be started ONCE – after it finishes, it cannot be restarted
  • The thread's state goes: NEW → RUNNABLE → (running) → TERMINATED
  • After TERMINATED, calling start() is illegal
  • To run the same logic again, create a new Thread object

Definition

The second start() throws IllegalThreadStateException. A Thread can only be started once. After termination, create a new Thread to run the logic again.

Thread lifecycle:

  • NEW: Created but not started.
  • start(): Transitions to RUNNABLE.
  • TERMINATED: After run() completes.
  • Second start(): Thread is no longer in NEW state → IllegalThreadStateException.

A bank cashier's shift can only start once. After the shift ends, you cannot restart the same shift. You must schedule a new shift (create a new Thread).

Understanding the Concepts

The code

JAVA
Thread t = new Thread(() -> System.out.println("Running"));
t.start();  // OK: NEW → RUNNABLE
t.start();  // throws IllegalThreadStateException!

Output

TEXT
Running
Exception in thread "main" java.lang.IllegalThreadStateException

How to run again

JAVA
Runnable task = () -> System.out.println("Running");
new Thread(task).start();  // first time
new Thread(task).start();  // new Thread object, same Runnable – OK

What should a senior engineer know?

  • Use ExecutorService instead of raw Threads: executor.submit(task) reuses threads from a pool.
  • Virtual Threads (Java 21+): Thread.startVirtualThread(task) creates a lightweight thread each time.

What are common mistakes?

A common mistake is assuming the output without tracing through the code step by step.

Wrong approach:

JAVA
package com.bank.quiz;

public class OutputQuiz {
    public static void main(String[] args) {
        // Developers often guess the wrong output
        // because they skip the step-by-step evaluation
        String result = "Balance: " + 100 + 50;
        System.out.println(result);
        // Many guess: "Balance: 150"
        // Actual: "Balance: 10050" (left-to-right string concat)
    }
}

Correct reasoning:

JAVA
package com.bank.quiz;

public class OutputQuiz {
    public static void main(String[] args) {
        // Step 1: "Balance: " + 100 = "Balance: 100" (String + int = String)
        // Step 2: "Balance: 100" + 50 = "Balance: 10050" (String + int = String)
        String result = "Balance: " + 100 + 50;
        System.out.println(result);  // Balance: 10050

        // To get arithmetic: use parentheses
        String correct = "Balance: " + (100 + 50);
        System.out.println(correct);  // Balance: 150
    }
}

Always trace left to right and remember that once a String is involved in concatenation, everything after it becomes string concatenation too.

What are the edge cases?

Edge cases can cause unexpected behavior in banking applications. Here are some to watch for.

JAVA
package com.bank.edge;

public class EdgeCaseDemo {
    public static void main(String[] args) {
        // Edge case 1: null handling
        String accountId = null;
        try {
            System.out.println(accountId.length());
        } catch (NullPointerException e) {
            System.out.println("Null account ID");
        }

        // Edge case 2: empty vs null
        String emptyId = "";
        System.out.println(emptyId.isEmpty());  // true
        System.out.println(emptyId == null);     // false

        // Edge case 3: boundary values
        java.math.BigDecimal zero = java.math.BigDecimal.ZERO;
        java.math.BigDecimal negativeBalance = new java.math.BigDecimal("-0.01");
        System.out.println(zero.compareTo(negativeBalance) > 0);  // true
    }
}
TEXT
Null account ID
true
false
true

Always test with null values, empty strings, boundary values, and negative amounts in banking applications.

What are common interview follow-up questions?

Interviewers often follow up with these related questions:

  • How does this concept apply to a high-throughput banking transaction system?
  • What are the performance implications in a production environment?
  • How would you debug issues related to this concept?
  • What tools would you use to monitor this in production?
  • How does this behave differently in Java 8 vs Java 17+?

When answering follow-up questions, always relate your answer back to practical experience. For example, at National Bank of India, Rajesh debugged a production issue related to this concept by using JFR recordings and heap dumps. Providing concrete examples from banking systems shows the interviewer that you understand both the theory and the practice.

Visual Overview

DIAGRAM
  Thread states: NEW → start() → RUNNABLE → run() → TERMINATED
  Second start() on a TERMINATED thread → IllegalThreadStateException
  Fix: create a new Thread or use ExecutorService

Key Takeaway

A Thread can only be started once. The second start() throws IllegalThreadStateException. To run the same logic again, create a new Thread or use an ExecutorService.

One sentence to remember: Thread.start() once only – second call throws IllegalThreadStateException.

---

Share
Use ← → arrow keys to navigate