We can use interrupt() method of java.lang.Thread class to interrupt
a thread that is in sleep state. It will get InterruptedException to
wake up from the sleep.
Sample code is as follows:
public class ThreadInterrupt implements Runnable {
public void run() {
try {
Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException e) {
SOP(“Interrupted by exception!");
}
}
public static void main(String[] args) throws InterruptedException
{
Thread myThread = new Thread(new ThreadInterrupt(),
“myThread");
myThread.start();
SOP(“Sleeping in main thread for 10 seconds”);
Thread.sleep(10000);
SOP(“Interrupting myThread");
myThread.interrupt();
}
}
https://www.digitalocean.com/community/tutorials/thread-sleep-java