Minor update
- Added safe sleep method to Async class to allow for sleeping in thread without try-catch
This commit is contained in:
parent
0f8067a628
commit
a54ecb6a0f
13
.idea/libraries/asm_all_5_1.xml
generated
13
.idea/libraries/asm_all_5_1.xml
generated
@ -1,13 +0,0 @@
|
|||||||
<component name="libraryTable">
|
|
||||||
<library name="asm-all-5.1">
|
|
||||||
<CLASSES>
|
|
||||||
<root url="jar://$USER_HOME$/Downloads/asm-5.1/lib/all/asm-all-5.1.jar!/" />
|
|
||||||
</CLASSES>
|
|
||||||
<JAVADOC>
|
|
||||||
<root url="file://$USER_HOME$/Downloads/asm-5.1/doc/javadoc/user" />
|
|
||||||
</JAVADOC>
|
|
||||||
<SOURCES>
|
|
||||||
<root url="jar://$USER_HOME$/Downloads/asm-5.1/src.zip!/" />
|
|
||||||
</SOURCES>
|
|
||||||
</library>
|
|
||||||
</component>
|
|
@ -7,6 +7,5 @@
|
|||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
<orderEntry type="library" name="asm-all-5.1" level="project" />
|
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
@ -27,6 +27,38 @@ public class Async<T> {
|
|||||||
*/
|
*/
|
||||||
volatile Throwable t;
|
volatile Throwable t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create Async object for sleeping for a while.
|
||||||
|
* @param millis Milliseconds to wait.
|
||||||
|
* @param micros Microseconds to wait.
|
||||||
|
*/
|
||||||
|
private Async(long millis, int micros){
|
||||||
|
task = new Thread(()->{
|
||||||
|
new ThreadLocal<Async>().set(Async.this);
|
||||||
|
try {
|
||||||
|
Thread.sleep(millis, micros);
|
||||||
|
this.complete = true;
|
||||||
|
} catch (InterruptedException t1) {
|
||||||
|
if(!this.failed) {
|
||||||
|
this.failed = true;
|
||||||
|
this.t=t1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
task.setDaemon(true);
|
||||||
|
task.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create Async process with runnable.
|
||||||
|
* @param r Runnable to execute as new task.
|
||||||
|
*/
|
||||||
|
public Async(Runnable r){
|
||||||
|
task = new Thread(r); // Execute thread with runnable
|
||||||
|
task.setDaemon(true); // Ensure that process dies with program
|
||||||
|
task.start(); // Start task
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initiates an async task that invokes the defined method. If object is null, the method must be static.
|
* Initiates an async task that invokes the defined method. If object is null, the method must be static.
|
||||||
* Note that this is optimized for larger tasks id est tasks that take more than 5 milliseconds to process (preferably a minimum of 10 ms).
|
* Note that this is optimized for larger tasks id est tasks that take more than 5 milliseconds to process (preferably a minimum of 10 ms).
|
||||||
@ -170,7 +202,23 @@ public class Async<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks for dangerous calls to Async methods such as awaiting oneself (which would cause a freeze in that thread).
|
||||||
|
*/
|
||||||
protected void checkDangerousThreadedAction(){
|
protected void checkDangerousThreadedAction(){
|
||||||
if(this.equals(current())) throw new RuntimeException("Calling dangerous method from inside thread is forbidden!");
|
if(this.equals(current())) throw new RuntimeException("Calling dangerous method from inside thread is forbidden!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Safe method for delaying the current thread.
|
||||||
|
* @param millis Milliseconds to delay.
|
||||||
|
* @param micros Microseconds to delay.
|
||||||
|
*/
|
||||||
|
public static void sleep(long millis, int micros){ new Async(millis, micros).await(); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Safe method for delaying the current thread.
|
||||||
|
* @param millis Milliseconds to delay.
|
||||||
|
*/
|
||||||
|
public static void sleep(long millis){ sleep(millis, 0); }
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user