0

Why passing "this" to a Thread constructor is a not safe construction technique? If it is then what is the alternative ? Is it has something to do with testing or debugging ? I find some answers but didn't get it totally.

class NewThread implements Runnable {
    Thread t;
    NewThread(){ 
        t = new Thread(this,"My new Thread");
    }
}
TTT
  • 1,952
  • 18
  • 33
Hello World
  • 944
  • 2
  • 15
  • 39
  • 1
    it relates to thread safety because it might lead to a race condition. See this https://www.ibm.com/developerworks/library/j-jtp0618/ – maxx777 Apr 11 '14 at 20:48
  • possible duplicate of [Java: starting a new thread in a constructor](http://stackoverflow.com/questions/8057510/java-starting-a-new-thread-in-a-constructor) – Gray Apr 11 '14 at 20:49
  • 1
    I think you are trying to ask why you shouldn't publish `this` in the constructor of an object to another thread. – Gray Apr 11 '14 at 20:49
  • 2
    As @Gray suggests, there is no harm in passing `this` to a Thread constructor, just don't do it from the constructor belonging to the `this` reference. – Keppil Apr 11 '14 at 20:50
  • @maxx777: I have already seen that link but didn't get it. I am new to Thread, so i find it confusing. – Hello World Apr 11 '14 at 21:08

1 Answers1

5

There is nothing wrong with passing this to a thread constructor. I suspect that what you are referring to is passing this to a thread constructor, in your own constructor.

class MyClass implements Runnable {
   private final Integer value;

   MyClass() {
      new Thread(this);
      int i = 0;
      value = Integer.valueOf(20 / i);
   }

   @Override
   public void run() {
      System.out.println(value.intValue());
   }
}

In general it's a bad idea to "leak" a this-reference from your constructor, because if the constructor throws an exception, the object may be left in an inconsistent state.

More specifically, it's a bad idea to start a thread with this in the constructor, because the thread may start running before you have finished constructing your object. So, even if i was 1 above, run() might still throw because it might happen before value was assigned.

In some cases you can kludge around this by creating the thread in the last line of your constructor. But this is not foolproof, since that constructor might be called by a superclass. A better solution is:

class MyClass implements Runnable {
    ...
    MyClass start() {
        new Thread(this);
        return this;
    }
    ...
}

// Then construct like this:
MyClass foo = new MyClass().start();
Russell Zahniser
  • 16,188
  • 39
  • 30
  • "...bad idea to "leak" a this-reference...because if the constructor throws an exception..." True enough, but it's even worse when you leak _this_ to another thread. In that case, you can run into trouble even if the constructor does _not_ throw an exception. Imagine, right after the leak, the thread in the constructor loses its time slice. Now the other thread could run, and access the object, even though the object has not been fully constructed. – Solomon Slow Apr 13 '14 at 20:39