The difference between Java's Start and Runnable methods

The difference between the two methods 
1) start:
Usage: The
start method starts the thread, which truly realizes multi-threaded operation. At this time, there is no need to wait for the execution of the run method body code to complete and directly continue to execute the following code. A thread is started by calling the
start() method of the Thread class. At this time, the thread is in a ready (runnable) state and is not running. Once the cpu time slice is obtained, the run() method is executed. The method
run() here is called For the thread body, it contains the content of the thread to be executed, the Run method ends, and the thread terminates.
2) run: The
run()
method is just an ordinary method of the class. If the Run method is called directly, there is still only the main thread in the program, and there is still only one program execution path. It still needs to be executed sequentially or wait for the
run method. After the body is executed, the following code can continue to be executed, so that the purpose of writing the thread is not achieved. Summary: The thread can be started by calling the start method, and the run method is just
a common method call of the thread, or executed in the main thread. These two methods should be familiar. Put the code that needs to be processed in parallel in the run() method. The start() method starts the thread and will automatically call the
run() method, which is specified by the memory mechanism of the jvm. And the run() method must have public access and the return value type is void.

Comparison of the two methods:

In practice, the Runable interface is often implemented. On the one hand, because java only supports single inheritance, the Thread class cannot be inherited from other classes, and the Runable interface has only one run method; on the other hand, the results can be seen. Implementing the Runable interface is the real multithreading...


Reference link: https://zhidao.baidu.com/question/1496082367158609459.html











Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325164581&siteId=291194637