Use supplier instead of RunnableVal for sync
This commit is contained in:
parent
0a246fc90f
commit
446dc9c568
@ -110,5 +110,6 @@ subprojects {
|
|||||||
maven {url "http://maven.sk89q.com/artifactory/repo"}
|
maven {url "http://maven.sk89q.com/artifactory/repo"}
|
||||||
maven {url "http://repo.spongepowered.org/maven"}
|
maven {url "http://repo.spongepowered.org/maven"}
|
||||||
maven {url "https://repo.inventivetalent.org/content/groups/public/"}
|
maven {url "https://repo.inventivetalent.org/content/groups/public/"}
|
||||||
|
maven {url "http://dl.bintray.com/tastybento/maven-repo"}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
repositories {
|
repositories {
|
||||||
flatDir {dirs 'lib'}
|
flatDir {dirs 'lib'}
|
||||||
maven {url "http://dl.bintray.com/tastybento/maven-repo"}
|
|
||||||
}
|
}
|
||||||
dependencies {
|
dependencies {
|
||||||
compile project(':core')
|
compile project(':core')
|
||||||
|
@ -9,6 +9,7 @@ import java.util.Iterator;
|
|||||||
import java.util.concurrent.ForkJoinPool;
|
import java.util.concurrent.ForkJoinPool;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
public abstract class TaskManager {
|
public abstract class TaskManager {
|
||||||
@ -284,6 +285,10 @@ public abstract class TaskManager {
|
|||||||
return sync(function, Integer.MAX_VALUE);
|
return sync(function, Integer.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <T> T sync(final Supplier<T> function) {
|
||||||
|
return sync(function, Integer.MAX_VALUE);
|
||||||
|
}
|
||||||
|
|
||||||
public void wait(AtomicBoolean running, int timout) {
|
public void wait(AtomicBoolean running, int timout) {
|
||||||
try {
|
try {
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
@ -380,16 +385,19 @@ public abstract class TaskManager {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public <T> T sync(final RunnableVal<T> function, int timeout) {
|
public <T> T sync(final RunnableVal<T> function, int timeout) {
|
||||||
|
return sync((Supplier<T>) function, timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> T sync(final Supplier<T> function, int timeout) {
|
||||||
if (Fawe.get().getMainThread() == Thread.currentThread()) {
|
if (Fawe.get().getMainThread() == Thread.currentThread()) {
|
||||||
function.run();
|
return function.get();
|
||||||
return function.value;
|
|
||||||
}
|
}
|
||||||
final AtomicBoolean running = new AtomicBoolean(true);
|
final AtomicBoolean running = new AtomicBoolean(true);
|
||||||
RunnableVal<RuntimeException> run = new RunnableVal<RuntimeException>() {
|
RunnableVal<Object> run = new RunnableVal<Object>() {
|
||||||
@Override
|
@Override
|
||||||
public void run(RuntimeException value) {
|
public void run(Object value) {
|
||||||
try {
|
try {
|
||||||
function.run();
|
this.value = function.get();
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
this.value = e;
|
this.value = e;
|
||||||
} catch (Throwable neverHappens) {
|
} catch (Throwable neverHappens) {
|
||||||
@ -412,9 +420,9 @@ public abstract class TaskManager {
|
|||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
MainUtil.handleError(e);
|
MainUtil.handleError(e);
|
||||||
}
|
}
|
||||||
if (run.value != null) {
|
if (run.value != null && run.value instanceof RuntimeException) {
|
||||||
throw run.value;
|
throw (RuntimeException) run.value;
|
||||||
}
|
}
|
||||||
return function.value;
|
return (T) run.value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user