Why can't we directly call the run() method?
Asked Answered
W

7

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?

Wolpert answered 28/1, 2011 at 16:1 Comment(6)
#263316Florrieflorry
This is a very basic question which could be answered by a very basic internet-search... @birryree: That is a very different question.Phira
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.Sapient
@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: #263316Florrieflorry
@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.Phira
@H.B. strange, I thought I linked to the answer: #263316 - guess I didn't correctly copy. Whoops!Florrieflorry
G
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.

Guitarist answered 28/1, 2011 at 16:5 Comment(2)
I am sorry I can't understand the meaning of last line. where didn't I include it when I wrote the run method?Reft
@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.Guitarist
L
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.

Lowspirited answered 28/1, 2011 at 16:4 Comment(0)
S
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.

Sapient answered 28/1, 2011 at 16:4 Comment(0)
D
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.

Dissociate answered 17/11, 2016 at 13:14 Comment(1)
nice and simple example.Hokusai
R
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.

Randell answered 20/12, 2013 at 13:31 Comment(0)
A
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

Asha answered 21/2, 2016 at 12:31 Comment(0)
K
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.

Kuhn answered 15/12, 2016 at 17:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.