The difference between thread run() and start()

What is the difference between run() and start() of a thread?

  • Calling the start() method is used to start the thread. When it is the turn of the thread to execute, run() will be called automatically; calling the run() method directly cannot achieve the purpose of starting multi-threading, which is equivalent to the main thread linearly executing the run() method of the Thread object.
  • The start() method of a thread-to-line can only be called once, and java.lang.IllegalThreadStateException will be thrown if it is called multiple times; there is no limit for the run() method.
    总结:The run() method is equivalent to starting a common method in the Thread class. start() is used to start the thread and then call the run() method when it is the thread's turn to execute. As for when it is the thread's turn to execute, it is determined by the cpu scheduling.

Test the run() method
Test the run() method
Test the start() method
insert image description here

Guess you like

Origin blog.csdn.net/ToBeMaybe_/article/details/117780311