12

If the start() method of a thread internally calls the run() method, then why don't we directly call the run() method in our code? What are the issues involved in doing so?

n_g
  • 3,315
  • 8
  • 25
  • 29
  • http://stackoverflow.com/questions/262816/when-would-you-call-javas-thread-run-instead-of-thread-start – wkl Jan 28 '11 at 16:05
  • This is a very basic question which could be answered by a very basic internet-search... @birryree: That is a very different question. – H.B. Jan 28 '11 at 16:06
  • 1
    That doesn't matter, HB. If it's programming-related and not been asked _here_ before, it belongs here. That way, internet searches for programming-related stuff will be directed here instead of those dodgy AskJeeves/ExpertSexChange sites. – paxdiablo Jan 28 '11 at 16:08
  • @H.B. - the question I linked to is different, but the answers provide the same insight into the differences between `start()` and `run()`, like in this answer: http://stackoverflow.com/questions/262816/when-would-you-call-javas-thread-run-instead-of-thread-start – wkl Jan 28 '11 at 16:10
  • @paxdiablo: Guess that's a fair point. @birryree: You linked to the question again but i understand what you mean, there are several other question which also have answers that would answer this one. – H.B. Jan 28 '11 at 16:19
  • @H.B. strange, I thought I linked to the answer: http://stackoverflow.com/questions/262816/when-would-you-call-javas-thread-run-instead-of-thread-start/262829#262829 - guess I didn't correctly copy. Whoops! – wkl Jan 28 '11 at 16:46

7 Answers7

19

The start method makes sure the code runs in a new thread context. If you called run directly, then it would be like an ordinary method call and it would run in the context of the current thread instead of the new one. The start method contains the special code to trigger the new thread; run obviously doesn't have that ability because you didn't include it when you wrote the run method.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • I am sorry I can't understand the meaning of last line. where didn't I include it when I wrote the run method? – ruben Jan 30 '11 at 22:04
  • 1
    @Newcomer, you didn't include it *anywhere* — that's the point. *You're* not the one writing code to make things run in a new thread. That's the job of the `Thread` class, with its JVM native code that can deal directly with threads and interact with the underlying OS. Since *you* don't write that special code in `run`, it should be obvious that *your* code won't perform any of those special operations. The `start` method makes that happen. It will call `run` for you; you just worry about what the thread should do *after* the JVM starts running it for you. – Rob Kennedy Jan 31 '11 at 04:06
3

Calling run executes the code synchronously; whereas allowing the JVM to call run via start would allow the code to execute asynchronously.

Calling run directly is often times beneficial in a testing situation where threading may want to be avoided.

Aaron McIver
  • 24,527
  • 5
  • 59
  • 88
2
class A implements Runnable
{
    public void run()
    {
        for( int i=0; i<5; i++)
        {
            System.out.println("Thread-A " +i + " Thread Name: "  +Thread.currentThread().getName());
        }
    }
}
class B implements Runnable
{
    public void run()
    {
        for( int i=0; i<5; i++)
        {

            System.out.println("Thread-B " +i + " Thread Name: "  +Thread.currentThread().getName() );
        }
    }
}

class MyThread 
{
    public static void main(String [] args)
    {
        Thread t1 = new Thread(new A());
        Thread t2 = new Thread(new B());

        t1.run();
        t2.run();
        System.out.println("**********************************************************");
        t1.start();
        t2.start();
    }
}

Copy & Paste above code ...then run it,,,and see the difference in output..

Basically run() will just exceute its body in context of current thread (which is main here) But, start() make a call to OS to create a new thread. start() will invoke run() method in the context of newly created thread.

2

Because start() will do it as a separate thread. If you were to just call run(), that would be part of your thread (i.e., a function call).

And, given that your thread may be an infinite loop waiting for work, that would be a bad thing.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

Calling run method directly will run that code in the main thread. Then it is like your program will be having only one thread (i.e main thread given by O.S).

If you call start method, which will call driver layer thread manager to create a thread for you, and from there your run function will be called. And hence your run method will be executed in a separate thread. Not in main thread.

user1923551
  • 4,684
  • 35
  • 29
0

The idea behind the thread is to create new stack everytime new thread starts running.

Invoking the run() method from main thread, the run() method goes onto the current call stack rather than at the beginning of a new call stack.

Exaple for the problem if you directly call run() method :

class TestCallRun2 extends Thread{  
 public void run(){  
  for(int i=1;i<5;i++){  
    try{Thread.sleep(500);}catch(InterruptedException e){System.out.println(e);}  
    System.out.print(i+" ");  
  }  
 }  
 public static void main(String args[]){  
  TestCallRun2 t1=new TestCallRun2();  
  TestCallRun2 t2=new TestCallRun2();  

  t1.run();  
  t2.run();  
 }  
}

Output:

1 2 3 4 5 1 2 3 4 5

Krutik
  • 1,107
  • 13
  • 18
0

Although calling run() directly is legal but it will defeat the purpose of multi threading. A thread works independently by having its own calling stack, if we do not use start() method than the executing stack for that statement would be the current stack through which that statement is running (main() method in most of the cases). This will defeat the purpose of running a job simultaneously while our main() method or in other words primary stack is running.

Nikhil Arora
  • 329
  • 3
  • 9