Archived
0

Changed runner blocking mechanism

This commit is contained in:
Daniil
2018-08-04 23:46:58 +07:00
parent 932682b6e1
commit 60cbec2119
3 changed files with 17 additions and 25 deletions

View File

@@ -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();
}

View File

@@ -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() {

View File

@@ -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() {
service.addTask(() -> {
testVariable.set(true);
latch.countDown();
}
});
latch.await(1, TimeUnit.SECONDS);