In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the sample analysis of interrupt interrupts in Java thread Thread. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Interrupt thread
The thread's thread.interrupt () method interrupts the thread, which sets the thread's interrupt status bit to true. Whether the resulting thread dies, waits for a new task, or continues to run to the next step depends on the program itself. The thread detects this interrupt flag bit from time to time to determine whether the thread should be interrupted (whether the interrupt marker value is true). It does not interrupt a running thread like the stop method.
Determine whether the thread is interrupted
To determine whether a thread has been sent an interrupt request, use the Thread.currentThread () .isInterrupted () method (because after it sets the thread interrupt marker bit to true, it does not immediately clear the interrupt flag bit, that is, it does not set the interrupt flag to false), instead of using the thread.interrupted () (which clears the interrupt flag bit after the method call, that is, it is reset to false), the following is how the thread interrupts when it is in the loop:
While (! Thread.currentThread (). IsInterrupted () & & more work to do) {do more work}
Interrupt status marking of interrupt
There are the following methods in the interrupt interrupt mechanism:
Thread.interrupt (), which sets the current interrupt marked as true (the set method of a similar property)
Thread.isInterrupted (), which detects the current interrupt flag (get method of similar attributes)
Thread.interrupted (), detects the current interrupt flag, and then resets the interrupt flag as false (get method with similar attributes + set method)
Therefore, the interrupt interrupt mechanism does not really interrupt the current thread, but a change in the interrupt flag. Let's test it with an example.
Public class InterruptTest {/ / the time consumed for printing private static long time = 0; private static void resetTime () {time = System.currentTimeMillis ();} private static void printContent (String content) {System.out.println (content + "time:" System.currentTimeMillis ()-time);} public static void main (String [] args) {test1 ();} private static void test1 () {Thread1 thread1 = new Thread1 (); thread1.start () / / interrupt interrupt try {Thread.sleep (3000);} catch (InterruptedException e) {e.printStackTrace ();} thread1.interrupt (); printContent ("execution interrupt") after 3 seconds delay;} private static class Thread1 extends Thread {@ Override public void run () {resetTime (); int num = 0; while (true) {if (isInterrupted ()) {printContent ("current thread isInterrupted"); break;} num++ If (num) {printContent ("num:" + num);}}}
The above code starts a Thread1 thread and adds 1 to the num in the Thread1 thread's while loop, printing every multiple of 100 (to prevent printing too fast). The main thread then calls the thread's interrupt method after 3000 milliseconds of sleep. So let's look at the output:
Intterupt interrupt
You can see that it takes about 3000 milliseconds, that is, after the main thread sleep, to execute thread1.interrupt (); later, the Thread1 thread stops, while the Thread1 thread stops because the isInterrupted method in the while loop returns true, so break exits the while loop, which means that interrupt and isInterrupted play the role of setXX and getXX here, maintaining a boolean variable.
Interrupt exception handling of interrupt
Of course, the interrupt mechanism is not only a change and detection of interrupt state bits, it can also handle interrupt exceptions. We know that the Thread.sleep () method needs to catch interrupt exceptions, so let's add a sleep delay to it.
While (true) {if (isInterrupted ()) {printContent ("current thread isInterrupted"); break;} num++; / / sleep the following try {Thread.sleep (1);} catch (InterruptedException e) {e.printStackTrace ();} if (num% 100 = = 0) {printContent ("num:" + num);}}
Let's take a look at the output:
Intterupt interrupt
Here we will find that after sleep sleep, the output num value is significantly smaller (the num has reached 1 billion when there is no sleep, it seems that CPU performs simple operations is still very fast), , but this is not the point, the point is to see an exception in the output, and after the output exception, the isInterrupted output returns to the false,Thread1 thread and continues to execute without exiting the while loop. So why is that? We just added a sleep sleep.
If there are operations in the Thread1 thread that need to catch InterruptedException exceptions, such as Thread's sleep,join method, Object's wait,Condition 's await, etc., it forces the need to catch InterruptedException exceptions, then when the thread1.interrupt method is called, it will throw an InterruptedException exception to thread1 thread, so in the while loop, it can catch the exception and immediately reset the thread interrupt ID to false after the exception is thrown So the next time the isInterrupted is judged in the while loop, it is false, it will not be break, and then the while loop will continue to execute.
So the interrupt () method does different things depending on whether there is code in the run method in the thread thread that must catch the InterruptedException exception:
If there is no code that must catch InterruptedException exceptions (such as Thread.sleep ()), isInterrupted () returns true, and the interrupt change can be handled in isInterrupted's judgment.
If there is code that must catch an InterruptedException exception (such as Thread.sleep ()), an InterruptedException exception is thrown and caught, while the isInterrupted is reset to false, and the interrupt change has to be handled in the exception catch.
Application scenarios of interrupt
Interrupt is usually suitable for loop tag judgment in thread execution, such as
While (! isInterrupted ()) {...}
But if there is a block in this loop, the thread cannot determine the next isInterrupted tag, so even if the interrupt () method is called, it cannot exit the loop and cannot exit the thread. For example
While (! isInterrupted ()) {... While (true) {/ / thread is stuck here, so it cannot respond to the interrupte mechanism}}
In this way, there is nothing interrupt can do, and the thread will continue to execute without being interrupted and stopped.
This is the end of this article on "sample analysis of interrupt interrupts of Java thread Thread". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.