Java multithreaded thread implementation method code details

Previously, I briefly introduced the use of Java multithreading, including thread class and runnable class. In order to better understand multithreading, this paper analyzes thread in detail.

start()

Let's take a look at the introduction of this method in the API:

Start the thread to execute; The Java virtual machine calls the run method of the thread.

The result is that two threads run concurrently; The current thread (returned to the start method from the call) and another thread (executing its run method).

It is illegal to start a thread multiple times. Especially when the thread has finished execution, it cannot be restarted.

Use the start method to start the thread, which really realizes multi-threaded operation. At this time, you don't need to wait for the execution of the run method body code, but directly continue to execute the following code. Start a thread by calling the start () method of the thread class. At this time, the thread is in a ready (runnable) state and does not run. Once the CPU time slice is obtained, it starts to execute the run () method. Here, the method run () is called the thread body, which contains the content of the thread to be executed. When the run method runs, the thread terminates immediately.

The start method is a method to start a thread. After use, Java will create a new thread to execute the method in run. This is a small demo:

Execution result: it is over thread-1 start thread-0 start thread-2 start thread-0 end thread-1 end thread-2 end

Due to the randomness of multithreading, the results may be different every time. This is also what we need to pay attention to. The execution order of threads is inconsistent with the call order.

run()

The run method is to call the run method of runnable set by thread and modify the above Demo:

Execution result: the direct result of the main start main end main start main end main start main end it is over run method is very different from that of start. It is executed completely in sequence without starting a new thread.

stop()

The stop method is to forcibly stop the execution of the thread. It is unsafe. Do not use this method. When stop is called, the locked resources will be released, but this release is inconsistent and easy to cause program problems. If you want to control the stop of a thread, you can use a custom variable or the isinterrupted () method:

interrupt()

Interrupt is used to notify the thread that you have been interrupted, but the specific interrupt execution needs to be customized in the thread. You can even ignore it and continue execution. Specifically, interruptedexception will be thrown when the thread executes the join, wait and sleep methods.

Execution results:

isInterrupted()

Judge whether the thread is interrupted. After executing the interrupt method above, it will return true.

Setpriority (int newpriority) and getpriority () set the priority of the thread and get the priority of the thread. The resources allocated by the CPU are focused on the threads with high priority.

Execution results:

When the priorities are the same, T1 and T2 are completed almost at the same time. When the priorities are different, there are obvious differences.

getName()

Relatively simple, get the name of the thread.

Join () and join (long millis)

The function of the jion method is to wait for the thread to complete execution. Join (long millis) can set the maximum waiting time. For example, the main thread needs to wait for the sub thread to complete and obtain the results of the sub thread before continuing to execute. At this time, you can use the join method

Execution results:

summary

The above is all about the implementation method code of Java multithreaded thread in this paper. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out.

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
分享
二维码
< <上一篇
下一篇>>