Search code examples
javamultithreadingjava-threads

Don't understand output order of thread constructor, start and run methods


Why does this code always print this?

in start oops
in ctor oops 

And doesn't call the run method even though the thread already has been started. when thread started firstly start method called then run.

class MyThread extends Thread {
    public MyThread(String name) {
    this.setName(name);
    start();
    System.out.println("in ctor " + getName());
  }
  public void start() {
    System.out.println("in start " + getName());
   }
  public void run() {
    System.out.println("in run " + getName());
  }
}


class Test {

   public static void main(String []args) {
            new MyThread("oops");
            }
 }

Solution

  • This is because Thread.start is overriden and is never actually called. Try to add super.start(); to MyTread.start