multithreading - How to make a thread sleep for specific amount of time in java? -


I have a scenario where I sleep soda for a certain time.

Code:

  Runs Public Zero () {try do {// do Thread.sleep (3000); // Do something after getting up} Grip (Interrupted e) E. {// Interrupted exceptions are complete before the sleep time is complete. So how do I actually thread my thread for 3 seconds? }}  

Now how do I handle that case where I am trying to run the thread, before the completion of sleep is killed with an obstructed exception? Apart from this, the thread awakens after being interrupted and whether it goes to a runny state or when it is only runanbal, then does the flow go to catch the block?

When your thread is killed with interrupt, it interrupted expense Then you can see how much time the thread is sleeping and it shows how much time falls asleep. In the end, instead of swallowing the exception, it is a good practice to restore the interruption condition so that the code above the call stack can handle the above code.

  Public Zero Run () // // something // 3000ms (approximately) for a long time sleep; SOS = 3000; Long start, end, soya; Boolean interrupted; While (timeTrace> gt; 0) {start = System.currentTimeMillis (); Try {Thread.sleep (timeToSleep); break; } Grip (Interrupted E) E. {// How much time to work out, sleep = System.currentTimeMillis (); Soy = start ending; TimeToSleep- = Soya; Interrupted = true}} if (interrupted) {// stop interruption before restart thread. Trillion (). Interrupt (); }}  

Comments