1

I was working on Threads when a question struck to my mind..If we can directly call run() method with the object of a class like any ordinary method then why do we need to call Thread.start() to call run() method..I tried both the methods as shown and got same result with both of them

First attempt by calling run() method directly

class Abc extends Thread
{
   public void run()
   {
      for(int i=0;i<5;i++)
      {
         System.out.println("Abc");
      }
      try
      {
          Thread.sleep(100);
      }catch(Exception e)
      {
          System.out.println("Error : "+ e);
      }
   }
}

class Xyz extends Thread
{
    public void run()
    {
       try
       {
           for(int i=0;i<5;i++)
           {
               System.out.println("Xyz");
           }
           Thread.sleep(100);
       }catch(Exception e)
       {
            System.out.println("Error : "+ e);
       }
    }
}

public class ThreadDemo 
{
    public static void main(String[] args) 
    {
        Abc ob=new Abc();
        Xyz oc=new Xyz();
        ob.run();
        oc.run();
    }
}

Second attempt by calling Thread.start()

public class ThreadDemo 
{
    public static void main(String[] args) 
    {
        Abc ob=new Abc();
        Xyz oc=new Xyz();
        Thread t1,t2;
        t1=new Thread(ob);
        t2=new Thread(oc);
        t1.start();
        t2.start();
    }
}
Java Enthusiast
  • 654
  • 7
  • 19
  • thread.start() starts a new call of stack however if you try invoking run directly then it will be invoked under currently executing call stack, i.e. main itself... – ppuskar May 28 '14 at 09:04

5 Answers5

8

If you call run() directly, the code gets executed in the calling thread. By calling start(), a new Thread is created and executed in parallel.

liarspocker
  • 2,434
  • 3
  • 17
  • 26
  • That's what I am asking..why there is a need to create new thread if the work is done by the calling thread – Java Enthusiast May 28 '14 at 09:06
  • 1
    @gau123: did you even read what multithreading is used for? – Alok May 28 '14 at 09:08
  • @gau123 It's not the same behaviour in the background. If you want multithreading, you need to create a new Thread and run it in parallel. Otherwise there's no point in creating threads. Try playing around with the sleep numbers and you'll see the difference. – liarspocker May 28 '14 at 09:10
2

If you directly call run(), then all the work will be done on the main thread. By using Thread.start, then it will executed on a new thread other than the main thread ;)

HatemTmi
  • 1,068
  • 9
  • 16
1

To observe what the difference is, increase your sleep calls to something longer like 10 seconds. This will make it obvious that you need Thread.start to avoid the first task waiting on the second.

In the case where you use Thread.start, you will see that output of both threads appears immediately.

In the case where you use run, you will see that the output of the first appears, then a 10 second delay, then the output of the second.

Wim Coenen
  • 66,094
  • 13
  • 157
  • 251
  • +1 but you still need to explain the difference between both of them. There is no mention of single or multiple thread – Alok May 28 '14 at 09:13
  • yeah but since he didn't mention it anywhere in his question, we need to clarify the whole thing, right? Because from his question, it doesn't seem like he does know the difference – Alok May 28 '14 at 09:41
0

If you call run() method directly then code will run inline. To run the code in separate thread it is necessary to call Thread.start().

Ambrish
  • 3,627
  • 2
  • 27
  • 42
0

When you call start() method on thread reference it create span entirely context area. Then thread will have independent stack from where you are calling. More over start() invokes OS native thread to start it in separate memory context.

While run() method invocation considered as simple method call, and you won't have benefit of concurrency as its executing is current stack.

Alpesh Gediya
  • 3,706
  • 1
  • 25
  • 38