Why we call Thread.start() method which in turns calls run method?

后端 未结 12 1087
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 01:40

Why do we call the thread object\'s start() method which in turns calls run() method, why not we directly call run() method?

相关标签:
12条回答
  • 2020-11-29 02:21

    If you directly call run() method its body is executed in context of current thread. When you invoke start() method a new thread is created and run() method is executed in this new thread.

    0 讨论(0)
  • 2020-11-29 02:25

    I presume you are talking about starting a thread. If that's the case the reason you don't invoke the run method directly is that you then would be calling the method, and not starting the thread.

    0 讨论(0)
  • 2020-11-29 02:27

    Even if programmatically we are not creating any thread, For every application, O.S will create a default thread to execute its code with CPU.

    Calling run method directly will make that run method execute in that main thread given by O.S.

    But the intention of creating a thread class is to make sure that run method executes in a different thread. Unless thread manager of O.S creates a thread, your run method will not get executed in a separate thread. To request O.S to create the separate thread you have to call start() method which will send a request to O.S to create a thread. Once O.S creates a thread, then O.S will automatically call run method of your thread class in that newly created thread context. And hence your purpose of creating a separate thread and executing your run method in a separate thread will be served.

    If you call run method directly, then it is like O.S is not creating any thread for you, and default main thread will execute your run method. No point of creating a separate thread class for that!

    Hope I am clear. Let me know if you need more explanation to answer your question.

    Note: Though books say JVM creates threads, internally JVM will have to send a request to thread manager driver of O.S layer to create a new thread in its thread pool. That's why I use O.S term more here than JVM.

    0 讨论(0)
  • 2020-11-29 02:28

    If a thread has been instantiated but not started its is said to be in new state.
    Unless until a start() method is invoked on the instance of the thread, it will not said to be alive.
    If you do not call a start() method on the newly created thread instance thread is not considered to be alive.
    If the start() method is not invoked and the run() method is directly called on the Thread instance, the code inside the run() method will not run in a separate new thread but it will start running in the existing thread.

    SEE THE PROBLEM IN EXAMPLE

    class Multi 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.println(i);  
      }  
     }  
     public static void main(String args[]){  
      Multi t1=new Multi();  
      Multi t2=new Multi();  
    
      t1.run();  
      t2.run();  
     }  
    }  
    
    Output:1
           2
           3
           4
           5
           1
           2
           3
           4
           5
    

    As you can see in the above program that there is no context-switching because here t1 and t2 will be treated as normal object not thread object.

    0 讨论(0)
  • 2020-11-29 02:29

    The difference is that if we execute the run method by start() method, it creates a new thread where we can execute run method otherwise the run() method will be executed in the thread created by JVM in the public static void main() method. This is exactly the whole point of multithreading, to run a different action on new threads.

    0 讨论(0)
  • 2020-11-29 02:31

    Why do we call the thread object's start() method which in turns calls run() method

    No it doesn't. start() calls the operating system, which starts a new thread, which (to simplify greatly) calls the run() method. Meanwhile the start() method has already returned to its caller. They are not equivalent.

    0 讨论(0)
提交回复
热议问题