Interrupt Method
Interrupt literally means interruption, but in Java, the thread. Interrupt () method actually notifies the thread in some way and does not directly stop the thread. W
Always thought that executing the interrupt method would allow the thread to end and throw interruptedexception. Read the Java concurrency Programming today in the seventh chapter of the discovery is not so, in the beginning of this chapter mentioned
It's not easy to get tasks and threads to stop safely, quickly, and reliably.
Use interrupt () to break the threadWhen one thread is running, another thread can call the interrupt () method of the corresponding thread object to break it, and the method simply sets a flag in the target thread that indicates
If the Java program does not have only one thread of execution, the program will run at the end of all threads. Rather, the program runs the end of all non-daemon threads at the end of the run, or when one of the threads calls the System.exet () method.Java provides an interrupt mechanism that we can use to end a thread
at what interrupt is doing.When th1.interrput () is called, the thread's interruption status (interrupted status) is set. We can check the interruption status of this Boolean Type through thread. currentthread (). isinterrupted.In core Java, there is a saying: "There is no language requirement that an interrupted program should be terminated. A
JAVA learning notes (40)-daemon thread and interrupt thread, java learning notesDaemon thread
/** Daemon threads, that is, Daemon threads * generally run in the background to provide services for other threads, cannot exist separa
, we will notify the cancellation of other channels.cancellation/shutdown mechanismThe Java thread class defines the following methods:Public final void Stop ()This method looks like it can stop the thread, but this method is flagged in order to be obsolete, simply saying that we should not use it and can ignore it.In Java
How to interrupt the Java thread? Check the API. Isn't the interrupt () method used? If the Thread has been interrupted, Use Thread. currentThread (). isInterrupted () to return true/false: public class ThreadDemo extends
First, introduce
This article records some of the knowledge points of the interrupt mechanism in Java multithreading. It is mainly the difference between the Stop method, the interrupted () and the Isinterrupted () method, and makes a simple analysis from the realization of the source code.
There are 3 ways to terminate a running thread in
Java Multi-thread interrupt () method is associated with sleep (), join (), wait ()
About Java multithreading knowledge can see "Thinking in Java" in the multithreaded part and "Java Network Programming" 5th chapter of the Multith
1. Interrupt () Notes
Before describing how to terminate a thread, it is necessary to understand the interrupt () first.
For interrupt (), the Java DJK documentation is described as follows:
http://docs.oracle.com/javase/7/docs/api/
Interrupts this
..." ); }} Once Thread.Interrupt () in Listing C is called, the thread receives an exception and then escapes the blocking state and determines that it should stop. Running the above code will get the following output:Starting thread ...Thread running ...Thread running ...Thread
First, introduceThis article mainly records the use of thread breaks in the interrupt () method, and how to handle interruptedexception. It is a cautious and skillful job to handle interruptedexception anomalies.Due to the use of the Stop () method to stop the thread is very violent, the people thread run well, suddenl
Interrupt method
Interrupt is literally meant to be interrupted, but in Java the Thread.Interrupt () method actually notifies the thread in some way and does not abort the thread directly. What you do is determined by the person who writes the code, and usually we abort the
class, or the class's join (), join (long), join (long, int), sleep (long), or sleep (long , int) is blocked during the method, its interrupt state is cleared, and it will receive a interruptedexception. We can catch the exception and do some processing. In addition, the thread.interrupted () method is a static method that determines the interrupt state of the current
Start today with a deep study of Java threads, starting with the 3 functions of sleep,join,interrupt.
Let's take a look at the functions of these 3 functions.
1,sleep: Is a static method of the thread class, which is to block the current thread, the function's argument is blocking time, in milliseconds.2,
From: http://jay-kid.iteye.com/blog/557064
In Java Network Programming, we can see an example that thread. Interrupt () is used to close the waiting thread. I don't quite understand, so I checked the Java API.
If you are interested, you can first look at the API content and
, after the method is called, the break state becomes false).Because the blocked thread throws exception and resets the interrupt state after it is interrupted, it is sometimes disadvantageous because the interrupt state may be used as a criterion for other threads, so it is prudent to reset the state at the point where the exception is handled:Booleaninterrupted
Java-17.2 thread interruption (interrupt)
In this section, we will discuss thread interruption (interrupt ).
1. What is thread interruption (interrupt )?
When multiple threads are runni
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.