This should be a simple take for any Java Master. Me being a newbie just wanted to confirm one thing.
I have a class implementing Runnable and like many such classes its run() method has an infinite loop. I want to do some task and then sleep for a min and come back again.
What happens if an Interrupted Exception is encountered while a thread is sleeping?
What I think would happen is the thread being suspended and now the infinite loop is of no help to keep the thread runnning. I'd like to confirm if my understanding is correct or not.
If this is what happens, What would be a possible solution to start up the thread up again.?
Your understanding is mostly correct - When your thread is sleeping , if it gets interrupted , this will cause an InterruptedException
to be thrown - your code in run()
will have to catch it and do what it wants to do. The thread itself will not be suspended - because active execution continues on this thread.
You may want to continue the execution of the thread after you handle the InterruptedException
in your catch block.