Skip to content

Instantly share code, notes, and snippets.

@Shilo
Last active June 21, 2022 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shilo/d619945b8f53672a585f68bc15fda241 to your computer and use it in GitHub Desktop.
Save Shilo/d619945b8f53672a585f68bc15fda241 to your computer and use it in GitHub Desktop.
Java mimic Javascript timeout.
package de.xida;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class Utils {
public static ScheduledFuture<?> setTimeout(long delayMilliseconds, Runnable runnable) {
var executor = Executors.newSingleThreadScheduledExecutor();
return executor.schedule(runnable, delayMilliseconds, TimeUnit.MILLISECONDS);
}
public static boolean cancelTimeout(ScheduledFuture<?> scheduledFuture) {
if (scheduledFuture == null)
return false;
return scheduledFuture.cancel(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment