Changed runner blocking mechanism
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package mc.core.events.runner;
|
||||
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
public class ExecutorThread extends Thread {
|
||||
private EventExecutorService service;
|
||||
|
||||
@@ -23,11 +25,15 @@ public class ExecutorThread extends Thread {
|
||||
}
|
||||
|
||||
void executeTask(ResourceRunnable runnable) {
|
||||
runnable.lock();
|
||||
for (Lock lock : runnable.getLocks()) {
|
||||
lock.lock();
|
||||
}
|
||||
try {
|
||||
runnable.run();
|
||||
} finally {
|
||||
runnable.unlock();
|
||||
for (Lock lock : runnable.getLocks()) {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
runnable.after();
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package mc.core.events.runner;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
public interface ResourceRunnable extends Runnable {
|
||||
default void lock() {
|
||||
|
||||
}
|
||||
|
||||
default void unlock() {
|
||||
|
||||
default List<Lock> getLocks() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
default void after() {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package mc.core.events;
|
||||
|
||||
import mc.core.events.runner.EventExecutorService;
|
||||
import mc.core.events.runner.ResourceRunnable;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -17,22 +16,9 @@ public class EventExecutorTest {
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
EventExecutorService service = new EventExecutorService(1);
|
||||
service.start();
|
||||
service.addTask(new ResourceRunnable() {
|
||||
@Override
|
||||
public void lock() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unlock() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
testVariable.set(true);
|
||||
latch.countDown();
|
||||
}
|
||||
service.addTask(() -> {
|
||||
testVariable.set(true);
|
||||
latch.countDown();
|
||||
});
|
||||
|
||||
latch.await(1, TimeUnit.SECONDS);
|
||||
|
||||
Reference in New Issue
Block a user