Java Thread API — which two are valid constructors for java.lang.Thread?

Difficulty: Easy

Correct Answer: Thread(Runnable r, String name), Thread()

Explanation:


Introduction / Context:
The Thread class offers several constructors for supplying a target Runnable, a name, and an optional ThreadGroup. Recognizing valid signatures is a common certification topic.



Given Data / Assumptions:

  • We must identify exactly which listed constructor signatures exist in the JDK.


Concept / Approach:
Valid overloads include Thread(), Thread(Runnable), Thread(Runnable, String), Thread(ThreadGroup, Runnable), Thread(ThreadGroup, String), and Thread(ThreadGroup, Runnable, String), among others. There is no constructor that accepts an int priority, and the order of parameters matters for the ThreadGroup/Runnable variants.



Step-by-Step Solution:

Option 1: Thread(Runnable r, String name) → valid.Option 2: Thread() → valid.Option 3: Thread(int priority) → invalid (priority is set via setPriority()).Option 4: Thread(Runnable r, ThreadGroup g) → invalid ordering; valid form is Thread(ThreadGroup g, Runnable r).Option 5: Thread(Runnable r, int priority) → invalid; no such constructor.


Verification / Alternative check:
Javadoc for java.lang.Thread lists the available constructors and their parameter orders.



Why Other Options Are Wrong:
They either use unsupported parameter types or the wrong parameter order.



Common Pitfalls:
Confusing constructor signatures with setter methods like setPriority(); swapping Runnable and ThreadGroup positions.



Final Answer:
Thread(Runnable r, String name), Thread()

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion