-
-
Save electronicboy/ab5f565e9afb32997ac2f384d1dc5731 to your computer and use it in GitHub Desktop.
HI PURRFORMANCE ASYNC SLEEP UTIL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
USAFULL FAR MEH
THANKE YOUU FORE THISE MAGNINFICIOUS STUHFF
A bit of sarcasm was included in this comment