Search code examples
javamethodspause

Pause execution for x seconds while performing method in Java


How can I pause my application while a method is being executed?

Consider the example below:

private void myMethod() {
   // some lines of code
   this.player.activateEffect(true);
   // pause for 5 seconds
   this.player.activateEffect(false);
}

I want the activeEffect method to BE active for 5 seconds, using the pause.


Solution

  • Here is the code for sleep:

    try {
      Thread.sleep(5000L);
    } catch(InterruptedException ie) {
       // Exception handling code here, it is a bad practice to leave this scope empty
    }