Java create and end thread code example

This article describes the most basic method of how to create and end threads in Java, only for java beginners. Some advanced knowledge such as thread synchronization, scheduling and thread pool will be gradually deepened in subsequent chapters.

Create thread

There are two ways to create a normal thread: inherit the thread class or implement the runnable interface. Examples are as follows.

Method 1: inherit thread class

Example creation method:

Method 2: implement runnable interface

Example creation method:

Run thread

For the above two methods, the methods for calling the thread to start running are as follows.

Method example 1: new mythread1() start(); Method example 2: new thread (New myrunnable1()) start();

Stop thread

Please don't take a deprecated Java lang.Thread. Stop method, no matter what reason you need to pause or completely end the currently running thread.

Common ways to exit a thread are as follows.

Mode 1: thread exits automatically

After the thread is executed, it will exit automatically. For example, the network download thread automatically exits when the background download is completed.

Method 2: use interrupt to actively exit

In the main thread, the thread state is set by calling the interrupt () method of the thread object.

In the sub thread, the isInterrupted () method is called to determine the thread state. If true is returned, the thread can be terminated. In the following example code, there is a while loop in the thread. Each loop will judge the thread state. If it is true, stop the loop and exit the thread.

It is the best way to end the thread by calling the interrupt setting state in the main thread and getting the state through isInterrupted in the sub thread. Note that once interrupt is called in the main thread, the related sleep and wait methods in the child thread will throw an interruptedexception exception during execution. At this time, you can also catch this exception to end the thread.

Mode 3: active exit with preset flag

Preset an exit flag to end the thread by judging the value of the flag. This is common for tasks in a loop. At each cycle, the flag value is judged. The following example determines the mcancelflag flag in the thread while loop.

When the main thread wants to actively end the thread, call the cancelthread method of the thread to set mcancelflag.

If you need to actively exit the thread when the thread is executing, the best way is method 2. The user can also use method 3 according to the actual situation, such as more state control.

One sentence summary

Create a thread: inherit the thread class or implement the runnable interface.

End thread: call interrupt method.

summary

The above is all about the Java create and end thread code example in this article. I hope it will be helpful to you. Interested friends can continue to refer to this site: detailed explanation of Java multithreaded thread communication producer consumer mode and waiting wake-up mechanism code, simple implementation code of multithreaded deadlock and inter thread communication in Java programming, examples of Java programming using socket multithreading to access server file code, etc. if there are any shortcomings, please leave a message to point out. Thank you for your support!

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