Igor Soudakevitch
Igor Soudakevitch

Reputation: 671

Setting Thread's name in constructor

Which approach is more appropriate: setting a thread's name in a ctor by calling super(name) or by invoking setName(name)?

class MyThread extends Thread{
    MyThread(String name){
//        super(name);      // which one is preferable?
//        setName(name);
    }
    public void run(){ /* business logic */ }
}

Would calling setName() in a ctor involve side effects? The reason I'm asking is that most tutorials use super(name) but I remember watching a video on YouTube where a college prof was always putting setName() right in the ctor...

Upvotes: 0

Views: 385

Answers (1)

Vishwa Bhat
Vishwa Bhat

Reputation: 109

No major difference except that if you will not be able to call setName() after Thread instantiation if either thread's state is NEW or the access privileges to that thread is changed. In short, it's better to set name during Thread instantiation itself to avoid surprises.

Upvotes: 2

Related Questions