Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good idea to start a thread from its own constructor? [duplicate]

Can we call thread_object.start() from within a constructor of this same object?

Is this approach a good idea ?

like image 236
Saurabh Gokhale Avatar asked Jan 23 '26 03:01

Saurabh Gokhale


2 Answers

You can do that, but it is considered as bad practice. There is one paragraph about starting Threads in constructors in this article.

AS nicerobot stated in the comments, your question seem to be a duplicate of this. Have a look at Heath Borders answer there.

like image 166
Nils Schmidt Avatar answered Jan 25 '26 16:01

Nils Schmidt


Just out of interest - why do you extend Thread? Why not implement Runnable instead, you get more flexibility (e.g. can be executed within a thread you create or an ExecutorService, which is the preferred method).

like image 33
ishmeister Avatar answered Jan 25 '26 17:01

ishmeister



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!