Interrupting or stopping a sleeping thread

Suresh picture Suresh · Nov 24, 2010 · Viewed 60.7k times · Source

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?

Answer

Fredrik Wallenius picture Fredrik Wallenius · Nov 24, 2010

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