1

I learned that to start a thread the recommended way is to invoke start() method on thread object. But this in turn invokes the run() method. So why not directly invoke run() method?

Here's what I tried but I fail to bring any conclusion :

public class Tester {
      public static void main (String[] args) {
        Runner r = new Runner();

        Thread t1 = new Thread(r, "Thread A(runnable)");
        Thread t2 = new Thread(r, "Thread B(runnable)");

        Thread s1 = new Strider("Thread C");
        Thread s2 = new Strider("Thread D");

        t1.run(); // t1.start();
        t2.run(); // t2.start();
        s1.run(); // s1.start();
        s2.run(); // s2.start();
      }
    }

    class Runner implements Runnable {
      private int counter;
      public void run() {
        try {
          for (int i = 0; i != 2; i++) {
            System.out.println(Thread.currentThread().getName() + " Runnable: " 
                  + counter++);
            Thread.sleep(1000);
          }
        }
        catch(InterruptedException e) {
          e.printStackTrace();
        }
      }
    }

    class Strider extends Thread {   
      private int counter;
      Strider(String name)    {
        super(name);
      }
      public void run()   {
        try {
          for (int i = 0; i != 2; i++) {
            System.out.println(Thread.currentThread().getName() + ": " 
                + counter++);
            Thread.sleep(1000);
          }
        }
        catch(InterruptedException e)     {
           e.printStackTrace();
        }
      }
    }

OUTPUT (using start()) : varies

Thread A(runnable): 0
Thread C: 0
Thread B(runnable): 1
Thread D: 0
Thread B(runnable): 3
Thread D: 1
Thread A(runnable): 2
Thread C: 1

OUTPUT (using run()) : always remains same

main Runnable: 0
main Runnable: 1
main Runnable: 2
main Runnable: 3
main: 0
main: 1
main: 0
main: 1

Also kindly provide your opinion on why not to use run() directly ?

KNU
  • 2,560
  • 5
  • 26
  • 39
  • 1
    This [post](http://stackoverflow.com/questions/23861979/why-is-this-thread-affecting-the-main-thread/23861994#23861994) might help you. – Braj Jul 30 '14 at 12:48
  • 1
    calling `run()` method is just like a simple method call inside current thread. whereas `start()` method call start a new thread and calls `run()` internally as part of new thread. – Braj Jul 30 '14 at 12:49
  • @user3218114 that was very helpful but it seems to be an old answer - use of stop() – KNU Jul 30 '14 at 13:05
  • 1
    The fact that `Thread` exposes a public `run()` method can be seen as a historical mistake. – Holger Jul 30 '14 at 13:07
  • I think it's duplicate of http://stackoverflow.com/questions/8052522/why-we-call-thread-start-method-which-in-turns-calls-run-method – KNU Jul 30 '14 at 13:13

2 Answers2

2

Calling the run method directly does not start the thread but is a synchronous call like any other method call in Java. Hence the output remains always the same.

Only when you call the start method, actual Java infrastructure is involved and a new thread is created, which then calls the run method.

Ralf Wagner
  • 1,467
  • 11
  • 19
1

If you call the run method explicitly, no new thread is going to be started. Calling run() directly just executes the code synchronously (in the same thread), just like a normal method call.

The Thread class' run() method does nothing, so sub-classes should override the method with code to execute in the second thread. If a Thread is instantiated with a Runnable argument, the thread's run() method executes the run() method of the Runnable object in the new thread instead.

Depending on the nature of your threaded program, calling the Thread run() method directly can give the same output as calling via the start() method, but in the latter case the code is actually executed in a new thread.

Deepanshu J bedi
  • 1,530
  • 1
  • 11
  • 23
  • But in case of thread made using runnable it prints upto 3 while in case of extending thread it prints upto 1 as expected. The runnabel cases t1 and t2 seems to be confusing me. – KNU Jul 30 '14 at 13:02