Reputation: 137
java.lang.Thread class has a constructor which takes in only a string parameter as the thread name. It has a constructor which takes in only a Runnable target. But it also has a default constructor. This means the name or the Runnable target is not mandatory.
But this is a bit different when it comes to creating a Thread with a ThreadGroup. There is no constructor which takes only a ThreadGroup in. At least the name or the Runnable target goes in with the ThreadGroup.
Is there any reason for java.lang.Thread class to not have a constructor which only takes a ThreadGroup in?
Upvotes: 1
Views: 209
Reputation: 50041
If you need to create a Thread
with only a ThreadGroup
parameter, you can use the Thread(ThreadGroup, Runnable)
constructor with a null Runnable
, which will have the same effect. I.e.,
t = new Thread(threadGroup, (Runnable)null);
(The cast is necessary to disambiguate it from the Thread(ThreadGroup, String)
constructor.)
There's no absolute reason why Thread
couldn't have a constructor taking only a ThreadGroup
, but there are already eight constructors. Each one of them is a complexity and burden on testing and documentation and trying to figure out which one you're invoking. The ninth constructor would struggle to justify its existence. In fact, given how rarely used ThreadGroup
s are, it would be no loss if there were fewer overloads, not more.
Upvotes: 1