2

Is there any difference between creating a thread using the run() method as opposed to using the a constructor? I noticed that I can start the thread and it acts the same in both ways.

new Thread MyThread().start

For example, as a constructor:

public class MyThread extends Thread{
    public MyThread(){
        // Do something
    }
}

or as the run() method

public class MyThread extends Thread{
    public void run(){
        // Do something
    }
}

Is there any difference between constructor or run()? Thanks!

user2566468
  • 179
  • 2
  • 11
  • [Related](http://stackoverflow.com/a/5623327/645270) – keyser Jul 25 '13 at 23:16
  • Your question is phrased in a very imprecise way. That's not the constructor, that's the difference between `Thread#start()` and `Thread#run()`. – Matt Ball Jul 25 '13 at 23:17
  • @MattBall - Actually, the question is "_what is the difference between placing logic in the constructor vs placing it the run method when code that looks like this is executed_: `new MyThread().start();`? Of course his example code is full of syntax errors... – jahroy Jul 26 '13 at 01:23

4 Answers4

4

It does not act the same in both cases

These cases are entirely different.

First, you probably need to learn about Threads and non-blocking processes. A Thread is used to do something asynchronously. So if you wanted to do some background task whilst doing something else then you would use a Thread. A good example is a GUI; you need one Thread to listen for GUI events (mouse clicks, button presses) and another to do any long running processing.

Now, onto your examples.

In Java a Thread consists of a run method that executes asynchronously when the start method is called. So when overriding Thread you change the run method. In reality you should never override Thread, you should use the constructor that takes a Runnable. There are many reasons for this, you should read up on concurrency.

Any code you place in your Thread constructor will be executed in the Thread that calls your constructor so this is not called asynchronously.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • Reasons why `implements Runnable` is better than extending `Thread` are here: http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread - basically cleaner OO design – zapl Jul 25 '13 at 23:44
  • 1
    @zapl there are other, more subtle reasons. For example a `Thread` calls `notifyAll` on itself when it dies - this is how `join` works; if you extend `Thread` and synchronise on `this` you can end up with _really_ weird outcomes... – Boris the Spider Jul 25 '13 at 23:47
3

If you put the code in the run method, a new thread will be started upon invocation of start, which uses the run method as its starting point. If you put the code in the constructor, however, it will be run in the same thread as that which invoked the constructor, because a constructor is a special case of a method. Thus, if you want to start something in a new thread, put it in run, otherwise, put it in the constructor. Also, if you want to start a thread, never call Thread.run, because of the same reason not to put code in the constructor. Always call Thread.start().

tbodt
  • 16,609
  • 6
  • 58
  • 83
  • If you want to start something in a new thread, put your code in `Thread.run()` **AND** start the thread by using `Thread.start()`. – jahroy Jul 25 '13 at 23:55
1

The key difference is this:

The code in your constructor is executed immediately and synchronously when the constructor is invoked.

The program will stop and wait for that code to complete before moving on to the next line of code.

If you put the code inside run() method AND use Thread.start(), the code will be executed in a separate thread (i.e. it will run asynchronously).

Your program will continue to execute (moving to the next line of code immediately) while the code in your run() method runs in parallel.

This is helpful if the code in run() takes a very long time to execute.

That's because your program can continue to do other things while it waits for the thread to finish its work.

jahroy
  • 22,322
  • 9
  • 59
  • 108
0

There is a difference. The constructor that creates the Runnable or subclass thereof runs in the main thread.

When starting a thread using:

new Thread(myRunnable).start();

you'll actually have run( of myRunnable run in the new thread.

NB You'll want to have a reference to the thread object in many cases. This code example is merely illustrative

On another note, never, ever, ever, give a thread this if starting within a constructor. Your computer could explode, or asphyxiation, drowning, or poisoning may occur.

nanofarad
  • 40,330
  • 4
  • 86
  • 117