4

I have a thread sleeping for some time and doing some work continuously.

public void run() {
    while(true) {
        try {
            Thread.sleep(SOME_RANDOM_TIME);
        } catch (InterruptedException e) {
            return;
        }
        doSomeLabour();
    }
}

I have a bunch of these threads running on Executors. When I call shutdownNow on the executor, the threads are not getting terminated sometimes. All shutdownNow does is it just calls interrupt on all the running threads and doesn't accept any new threads.

So, basically, I'm just interrupting all the threads.

I understand, that when thread is not in sleep, and it's interrupted, nothing will happen in case of my code; however, shouldn't sleep(..) be throwing InterruptedException next time when the thread tries to sleep after done with the work because the interrupted flag is still set on that thread?

  • Please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Stephen C Jun 08 '20 at 04:47

1 Answers1

0

How about

while(!Thread.currentThread().isInterrupted()) {
    try {
        Thread.sleep(SOME_RANDOM_TIME);
    } catch (InterruptedException e) {
        return;
    }
    doSomeLabour();
}

instead?

JDK Javadoc states for Thread.interrupt():

If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.

So sleep() itself doesn't throw an InterruptedException, rather the interrupter does if the thread is currently sleeping.

Simon
  • 2,994
  • 3
  • 28
  • 37
  • That would work. But, I'm trying to understand how sleep throws InterruptedException. Does sleep throw InterruptedException only when the thread is interrupted while sleeping? – Karthik Chennupati Jun 08 '20 at 05:12
  • Your final sentence doesn't make sense. Of course it is the sleep that throws the exception. – user207421 Jun 08 '20 at 05:28
  • I wonder what's the point of interrupting a blocked thread? Is this exception thrown when the already blocked thread is attempted to be interrupted? – mangusta Jun 08 '20 at 05:29
  • @MarquisofLorne, sorry for not describing that clearly. The InterruptedException is not thrown/managed by the sleep() method, ie. it's not the sleep() method which does something like constantly checking the 'interrupted' flag. Rather, the InterruptedException is thrown by the interrupt() call, if the thread is blocked (eg. by sleep). The relevant part: if interrupt() encounters a non-blocked thread, there's no InterruptedException, even if that thread later chooses to sleep(). – Simon Jun 08 '20 at 05:41
  • @KarthikChennupati, I'm looking at the JDK source, eg. at http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/src/share/classes/java/lang/Thread.java, and reading the javadoc of the Thread class. – Simon Jun 08 '20 at 14:01