Java Thread Interruptedexception exception

Source: Internet
Author: User
Tags instance method thread class

When a method is declared that might throw a interruptedexception exception, the method is described as a method that may take a little time, but can be canceled.

The representative methods of throwing interruptedexception are:

1. The wait method for the Java.lang.Object class

2. The sleep method of the Java.lang.Thread class

3. Join method for the Java.lang.Thread class

The representative methods of throwing interruptedexception are:

1. The wait method for the Java.lang.Object class

2. The sleep method of the Java.lang.Thread class

3. Join method for the Java.lang.Thread class

-A way to take some time

The thread that executes the wait method enters the waiting area waiting to be notify/notify all. During the wait, the thread will not be active.

The thread that executes the sleep method pauses the time set in the execution parameter.

The thread that executes the join method waits until the specified thread ends.

Therefore, the method above is a way to take some time.

--Methods that can be canceled

Because the action that takes time reduces the responsiveness of the program, it may cancel/abort the execution of this method.

This is mainly done by the interrupt method to cancel.

1. Sleep method and Interrupt method

The interrupt method is an instance method of the thread class, which does not need to acquire a lock on the thread instance, and any thread can invoke the interrupt method of the other thread at any moment through the thread instance.

When a thread in sleep is called the interrupt method, the paused state is discarded and the interruptedexception exception is thrown, so that the control of the thread is given to the catch block that catches the exception.

2. The wait method and the interrupt method

When a thread calls the wait method, it touches the lock when it enters the waiting area. When the interrupt method is called on a thread in wait, the lock is retrieved first, then the interruptedexception exception is thrown, and the interruptedexception exception cannot be thrown until the lock is acquired.

3. Join method and Interrupt method

When a thread waits for another thread to end with the Join method, it can be canceled by using the interrupt method. Because the join method does not need to acquire a lock, as with sleep, it jumps immediately to the catch block

--What did the interrupt method do?

The interrupt method actually changes the interrupt state only.

The inside of these methods, sleep, wait, and join, constantly checks the value of the interrupt state to throw interruptedexception.

So, if the thread does other processing, it calls its interrupt method, and the threads do not throw interruptedexception, only when the thread goes to sleep, wait, join these methods, Before the interruptedexception is thrown. If you do not call sleep, wait, join these methods, or do not have the online thread themselves check the interrupt state, throw interruptedexception, that interruptedexception will not be thrown out.

Isinterrupted method, which can be used to check the interrupt status

The Thread.interrupted method can be used to check and clear the interrupt state.

From: 4845757

Java Thread Interruptedexception exception

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.