I am new to Java concurrecny and I am reading this at the moment: Java Tutorial-Interrupts But I can’t really understand where and why I should use an Interrupt. Can someone give me an example (code) so I better understand it? thx
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Interrupts are used when you want to (cough) interrupt the thread — typically meaning stop it from operating.
Thread.stop()has been deprecated because of various issues soThread.interrupt()is the way that you tell the thread that it should cease running — it should cleanup what it is doing and quit. In reality, the programmer can use the interrupt signal on a thread in any way that they want.Some examples:
There are certainly many ways to accomplish the above signaling but interrupt can be used.
One of the more powerful ways that
Thread.interrupt()affects a running thread is by throwingInterruptedExceptionfrom a couple different methods includingThread.sleep(),Object.wait(), and others.Also, often in a thread we are looping doing some task and so we check for interrupt status: