Q:

Interrupting or stopping a sleeping thread

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?

A:

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

A:

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.

A:

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. 

