I know that Thread.sleep(1000); can be used to freeze the current thread (for 1 second in the example), but since you need to throw an Exception I am wondering if it is safe to use it. I don’t want to use it in a program and accidentally cause problems.
Is it “ok” to use Thread.sleep(); or is there a better way to achieve the same outcome?
Thread#sleep only throws 2 exceptions:
IllegalArgumentExceptionif the parameter is negativeInterruptedExceptionif the thread gets interrupted=> if you pass a positive number and the thread doesn’t get interrupted there will be no exception.
Unless you call
theThread.interrupt()from another thread you will be fine.EDIT
It seems you want to create a timer – in that case you would make your life much simpler by using the built-in mechanisms: