Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java InterruptedException suspends thread?

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.?

like image 556
peterd Avatar asked Jul 19 '26 09:07

peterd


1 Answers

Wrong.
An InterruptedException would just terminate the sleep() call and throw an exception.
As long as you handle the exception appropriately, your thread will continue running.

like image 93
SLaks Avatar answered Jul 21 '26 00:07

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!