Two methods of starting thread start and run in Java

1、 Distinction

There are two ways to start a thread in Java: inheriting the thread class and implementing the runnable interface. Since Java cannot implement multiple inheritance, it is generally used to create a thread by implementing the runnable interface. However, either method can start the thread through the start () and run () methods. Let's introduce the differences between them.

Start method:

This method not only starts the thread, but also creates a thread, which truly realizes multithreading. Without waiting for the code in the run () method to execute, you can then execute the following code. At this time, the thread of start () is in a ready state. When the CPU time slice is obtained, the run () method will be executed. The run () method contains the contents of the thread to be executed. When the run () method runs, the thread will terminate.

Run method:

Starting a thread through the run method is actually calling a method in a class as an ordinary method. There is no thread created. There is still only one main thread in the program. You must wait until the code in the run () method is executed before continuing to execute the following code. In this way, the purpose of writing threads is not achieved.

Let's understand it through a very classic topic:

The code is shown in the figure. What should be the output when running the program? Yes, the output is "pong ping". Because t.run() actually waits for the run() method in the new thread to call pong() before continuing to print "Ping". It is not a real thread.

And if we put t.run(); Modify to t.start(); Then, the result is obviously "ping pong", because when it is executed here, a new thread t is created and ready, the code continues to execute and prints out "Ping". At this point, the execution is complete. Thread t obtains the time slice of CPU, starts execution, and calls the pong () method to print out "Pong".

If you are interested, you can add a few more sentences to see the effect yourself.

2、 Source code

What are the essential differences between them? Let's take a look at the source code:

It can be seen that when a thread starts, its status (threadstatus) is set to 0. If it is not 0, an illegalthreadstateexception will be thrown. Normally, add the thread to the thread group, and finally try to call the start0 method, which is a private native method (native method is an interface for java to call non java code).

I guess it is implemented in C. It seems that calling the bottom layer of the system still needs to be through C language. This is why the start () method can implement multithreading. Calling the run () method is actually just calling the run () method implemented in runnable.

Let's look at the source code of run() in thread:

If the target is not empty, call the run () method of the target. What is the target

In fact, it is a runnable interface, just like the new thread in the above code. In fact, we are implementing its run () method. Therefore, if you call run directly, it is no different from an ordinary method. A new thread will not be created because the start0 method is not executed at all.

3、 Realize

As mentioned earlier, both inheriting the thread class and implementing the runnable interface can define a thread, so what's the difference between them? It is mentioned on page 627 of Java core technology Volume 1, 9th Edition. The subclass of thread can be built through the following code to define a thread:

Then, instantiate an object and call its start method. However, this method is not recommended. The number of tasks that need to run in parallel should be reduced. If there are many tasks, the cost of creating an independent thread for each task is too much. Of course, thread pool can be used to solve it.

Advantages of implementing runnable interface:

1. Avoid the problem of Java single inheritance

2. It is suitable for multithreading to process the same resource

3. The code can be shared by multiple threads, the data is independent, and it is easy to realize resource sharing

To sum up:

1. Start () can start a new thread, but run () cannot

2. Start () cannot be called repeatedly, run () can

3. The run code in start () can continue to execute the following code without completing execution, that is, thread switching is performed. Directly calling the run method must wait for all its code to execute before continuing to execute the following code.

4. Start () implements multithreading, while run () does not.

The above is the method of starting thread start and run in Java introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>