In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of Java thread termination instance analysis, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this Java thread termination instance analysis article. Let's take a look at it.
Topic analysis
C is the answer.
A: the thread uses the sleep () method to suspend the thread for a period of time, not to terminate
B: when you create a new thread, it has no effect on the previous thread
C: an exception is thrown and the thread terminates
D: it is not termination, but preemption. The process is the most basic unit of resource allocation. Different threads created by the same process share these resources. When one thread has a higher priority, it will preempt the resources of other threads. As a result, other threads have no resources available, resulting in blocking.
Three reasons for thread termination
1. The execution of the run method is completed and the thread ends normally.
2. The thread throws an uncaptured Exception or Error
3. Call the thread's Stop method directly to end the thread (not recommended, as it can easily lead to deadlock)
Concrete analysis
Three methods of thread termination
① exits the thread using the flag bit
② uses the stop method to force thread termination
③ uses interrupt to terminate threads
1. Use flag bits to push out the thread
Usually, when the run () method is executed, the thread ends normally. However, often some threads are servo threads. They need to run for a long time, so they use a variable to control the loop.
An exit flag exit is defined. When exit is true, the while loop exits, and the default value of exit is false. When defining exit, a Java keyword volatile is used to synchronize the exit. Only one thread can modify the value of the exit at a time.
Public class ThreadSafe extends Thread {public volatile boolean exit = false; public void run () {while (! exit) {/ / do something}} 2, use the stop method to force thread termination
Thread.stop () can be used directly in the program to forcibly terminate the thread, but the stop method is very dangerous, just like suddenly shutting down the computer instead of shutting down according to the normal program, it may produce unpredictable results, the main insecurity is: after thread.stop () call, the thread that creates the child thread will throw the ThreadDeatherror error and release all locks held by the child thread.
3. Use interrupt to terminate the thread
Use interrpt to terminate when the thread is in two states
(1) the thread is not in a blocking state:
Use isInterrupted () to determine the thread's interrupt flag to exit the loop, and then use the interrupt () method, the interrupt flag will be set to true, just like using a custom flag to control the loop
Public class MyThread extends Thread {@ Override public void run () {try {for (int I = 0; I < 500000; iTunes +) {if (interrupted ()) {System.out.println ("already stopped"); throw new InterruptedException () / / interrupt exception} System.out.println ("I =" + (I + 1));} System.out.println ("I'm under for");} catch (InterruptedException e) {System.out.println ("catch in the run method!") ; e.printStackTrace ();}
(2) Thread is in blocking state: call the thread's interrupt () method when using the sleep method, receiver,accept in the wait,socket of the synchronization lock, etc.
Public class ThreadInterrupt extends Thread {public void run () {try {sleep (50000); / / delay 50 seconds} catch (InterruptedException e) {System.out.println (e.getMessage ());}} public static void main (String [] args) throws Exception {Thread thread = new ThreadInterrupt (); thread.start () System.out.println ("interrupt the thread by pressing any key within 50 seconds!"); System.in.read (); thread.interrupt (); thread.join (); System.out.println ("Thread has exited!");} this article on "Java thread termination instance analysis" ends here, thank you for reading! I believe you all have a certain understanding of the knowledge of "Java thread termination instance analysis". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.