Skip to content

Instantly share code, notes, and snippets.

@electronicboy
Last active September 12, 2018 21:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save electronicboy/ab5f565e9afb32997ac2f384d1dc5731 to your computer and use it in GitHub Desktop.
Save electronicboy/ab5f565e9afb32997ac2f384d1dc5731 to your computer and use it in GitHub Desktop.
HI PURRFORMANCE ASYNC SLEEP UTIL
package pw.valaria;
public class HiPurrformanceSleepUtil {
public static void sleep(Long sleep) {
try {
new SleepThread(sleep, Thread.currentThread());
throw new InterruptedException(); // Tell java that it is wrong, and mah CODE IS AWESUM
} catch (InterruptedException ignored) {}
}
private static class SleepThread implements Runnable {
Long sleep;
Thread callingThread;
SleepThread(Long sleep, Thread thread) {
this.sleep = sleep;
this.callingThread = thread;
new Thread(this).run();
// time to start a new thread removes the potential for a race condition. Who cares?!
try {
wait();
// WE KNO WAT WE DOIN, PLZ
} catch (InterruptedException | IllegalMonitorStateException ignored) {
}
}
@Override
public void run() {
try {
Thread.sleep(sleep);
callingThread.interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
@NickAcPT
Copy link

USAFULL FAR MEH
THANKE YOUU FORE THISE MAGNINFICIOUS STUHFF

A bit of sarcasm was included in this comment

@MisterFixx
Copy link

my name jeff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment