Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interrupting or stopping a sleeping thread [duplicate]

How to stop or interrupt a sleeping thread in java.? I have a thread that syncs data and sleeps for 10 minutes in run() method, if i want to stop the sync by stopping the thread when it is sleeping.? How can this be achieved?

like image 723
Suresh Avatar asked Nov 24 '10 07:11

Suresh


People also ask

Can you interrupt a sleeping thread?

interrupt() method: If any thread is in sleeping or waiting for a state then using the interrupt() method, we can interrupt the execution of that thread by showing InterruptedException. A thread that is in the sleeping or waiting state can be interrupted with the help of the interrupt() method of Thread class.

Does interrupt stop a thread?

interrupt() does not interrupt the thread, it continues to run.

What is interrupting a thread?

An interrupt is an indication to a thread that it should stop what it is doing and do something else. It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate.

Is thrown when a thread is interrupted while sleeping or waiting?

An InterruptedException is thrown when a thread is interrupted while it's waiting, sleeping, or otherwise occupied. In other words, some code has called the interrupt() method on our thread. It's a checked exception, and many blocking operations in Java can throw it.


4 Answers

Call the interrupt() method on your thread. This will cause the sleep to be cancelled and an InterruptedException will be thrown.

like image 76
Fredrik Wallenius Avatar answered Oct 17 '22 21:10

Fredrik Wallenius


You can interrupt a sleeping thread with Thread.interrupt(). Now stopping a thread is deprecated - what if the thread is holding a lock to something? (There was a Thread.stop(), but it is HIGHLY discouraged).

Alternatively, instead of sleeping, you can have your thread wait for an object to be notified with a timeout, and simplify notify the object if you want the thread to wake up.

like image 20
EboMike Avatar answered Oct 17 '22 21:10

EboMike


Suresh, I wish to pay attention to one issue. interrupt() methods itself does not interrupt thread. It just sends a request to a thread for interruption, by setting the flag to true. Usually your thread should process cancellation/interruption policy. It is greatly described in Java Concurrecy in Practice, section 7.1 Task Cancellation.

Blocking library methods such Thread.sleep and Object.wait try to detect when a thread has been interrupted and return early. They respond to interruption by clearing the interrupted status and throwing InterruptedException.

like image 6
Alexandr Avatar answered Oct 17 '22 21:10

Alexandr


Since I can't comment, I'll post another answer. I just want to reinforce and clarify what Alexandr said. interrupt() only sets a flag in the Thread, and the extended Thread or Runnable object have to check if it have been interrupted with Thread.interrupted() to do what it's supposed to do when interrupted.

interrupt() also stops a wait() or sleep(), but pay attention that those methods UNSET the interrupt flag. So, if you call interrupt() on a sleeping/waiting thread, it will stop sleeping/waiting but will not know that it was interrupted, except if you catch the exception, and will not continue to be interrupted.

I was able to fix a program of mine by implementing my own interrupt flag, which couldn't be overridden by the sleep() and wait() methods. Also, know that you can interrupt a thread from within with Thread.currentThread().interrupt().

like image 4
GuiRitter Avatar answered Oct 17 '22 20:10

GuiRitter