1

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.

Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
t4dohx
  • 675
  • 4
  • 24
  • 1
    `Thread.sleep` *could* work if this is a linear console program or one where threading is not an issue. But if this code is part of an event-driven GUI, such as a Swing or JavaFX program, then it is easy to freeze the entire program, rendering it unresponsive by using `Thread.sleep` without care for which thread it is called on. You may want to provide more information about your program's structure. – Hovercraft Full Of Eels Nov 28 '16 at 17:27
  • check this out http://stackoverflow.com/a/24104427/6689101 – zombie Nov 28 '16 at 17:31

3 Answers3

2

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
}
Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
  • 3
    Thread.sleep will block the Thread and if it's running on the UI Thread then the application will freeze – zombie Nov 28 '16 at 17:25
  • Does anyone have idea why the program "freezes" for a certain amount of seconds when called from another method? If I call the method directly from instance interface it does not freeze and work well. – t4dohx Nov 30 '16 at 23:03
  • BTW, I wrote an Open Source Java library called MgntUtils where there is a TimeUtils class with method sleepFor(Long, TimeUnit). Inside it "swallows" exception so instead of the code above, you will have to do: TimeUtils.sleepFor(5L, TimeUnit.SECONDS); Inside it would do roughly the same as code in the answer, but this is more compact. Here is the link to the article that explains what utilities are in the library and where to get it (working jars, source code and java doc are available) https://www.linkedin.com/pulse/open-source-java-library-some-useful-utilities-michael-gantman/ – Michael Gantman May 23 '18 at 10:17
2

The whole idea of waiting inside a state changing method is flawed. Waiting delays the entire thread, preventing any work getting done.

If it happens to be the UI controlling thread, the application freezes.

What you really want to do is give the "effect" a timeout value, and with each (game) "tick" check if the timeout has expired and remove it when its expired.

Durandal
  • 19,919
  • 4
  • 36
  • 70
1

Your best bet is to perform your action in myMethod() then invoke another method, probably a Runnable, 5 seconds later.

private void myMethod() {
   // some lines of code
   this.player.activateEffect(true);

   // run afterDelay in 5 seconds
   postDelayed(afterDelay, 5000);
}

private Runnable afterDelay = new Runnable() {
    @Override
    public void run() {
        player.activateEffect(false);
    }
};

This code comes from my own experience with Android Java, but I think this technique is portable.

Bamaco
  • 592
  • 9
  • 25