3

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

Is this approach a good idea ?

5
  • 2
    Constructor of what? Can you show us some code to describe what exactly you are thinking of? May 6, 2010 at 10:18
  • 1
    Constructor of class that extended Thread class ... May 6, 2010 at 10:32
  • I think you mean "this.start() where this extends Thread" ^^
    – Hardcoded
    May 6, 2010 at 11:35
  • Are you asking: stackoverflow.com/questions/84285/… ?
    – nicerobot
    May 6, 2010 at 12:45
  • Use FindBugs, it will advice you on that and more bad practice May 6, 2010 at 12:50

5 Answers 5

6

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.

2
  • @Nils: The article only points out the risks in exposing 'this' reference when the object is not in a consistent state yet. In the case of threads, you can do it safely in the ctor by creating a non-inner-class thread, and providing it with the data it needs for running (and not with 'this' reference). May 6, 2010 at 11:35
  • Excellent! Reference escaping can is not thread-safe! May 6, 2010 at 13:10
2

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).

1
  • +1 for implementing runnable and the Java 5 concurrency APIs!
    – justkt
    May 6, 2010 at 14:27
1

It's a bad practice, since you cannot be sure that the object is fully initialized. Even if you call the start() method at the end of the constructor it may result in a mess.

Be aware that the processor can do things out of order:

1: MyObject(){
2:  aVariable = anyValue;
3:  this.start();
4: }

The processor is free to execute line 3 before line 2 since they aren't related (in a single threaded fashion), so you could end up with uninitalized variables (even final ones), and other unexpected stuff.

7
  • Actually, calling thread.start() is in a happens-before relationship to everything that happens in the run() method, and aVariable = anyValue happens-before starting the thread by being before it in the constructor. So your particular example is well defined, and will not cause any problems. Aug 31, 2011 at 23:18
  • No, since most of us are using out-of-order-processors, nothing is defined. JLS: "It should be noted that the presence of a happens-before relationship between two actions does not necessarily imply that they have to take place in that order in an implementation. If the reordering produces results consistent with a legal execution, it is not illegal." Since those two statements aren't directly affected by each other (accessing the same variable or similar), reordering is allowed.
    – Hardcoded
    Sep 2, 2011 at 6:57
  • Read the second part of your cited sentence: If the reordering produces results consistent with a legal execution. I.e. it must produce the same results. The processor can do things out-of-order, but the happens-before order is still derived from the program order (i.e. the order of the actions in one thread as written in the program) and the synchronization order (certain inter-thread ordering actions, here starting of a thread). (Linking section 17.4.3 to 17.4.5 for details for any readers.) Sep 2, 2011 at 12:00
  • If the second thread does not access the variable, you can reorder as you want - but there won't be any problem, as this variable is not accessed. If the second thread accesses the variable, the happens-before relationship guarantees that it reads the value set in the constructor (if there isn't someone setting another value between). Nothing undefined here. Sep 2, 2011 at 12:02
  • How does the processor or the JVM know what will happen in a different thread (maybe at a different processor) at a different method? happen-before is AFAIK associated to the in-method-calls. From that point of view, the implementation would be free to reorder and getting the same result (starting a thread and setting a value). Otherwise there would be a lot of beforehand-tracing and/or some magic involved. ;-)
    – Hardcoded
    Sep 2, 2011 at 12:29
0

I would suggest to start the thread outside, instead of calling from Constructor. In constructor you should be doing only initialization stuff.

-1

As long the thread you're starting doesn't have a reference to this then you're okay. If the thread does have a reference to this (either passed to the thread of indirectly via an inner class) then it's a bad idea to start it from the constructor as the object isn't fully initialized.

Not the answer you're looking for? Browse other questions tagged or ask your own question.