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; package mc.core.events.runner;
import java.util.concurrent.locks.Lock;
public class ExecutorThread extends Thread { public class ExecutorThread extends Thread {
private EventExecutorService service; private EventExecutorService service;
@@ -23,11 +25,15 @@ public class ExecutorThread extends Thread {
} }
void executeTask(ResourceRunnable runnable) { void executeTask(ResourceRunnable runnable) {
runnable.lock(); for (Lock lock : runnable.getLocks()) {
lock.lock();
}
try { try {
runnable.run(); runnable.run();
} finally { } finally {
runnable.unlock(); for (Lock lock : runnable.getLocks()) {
lock.unlock();
}
} }
runnable.after(); runnable.after();
} }

View File

@@ -1,12 +1,12 @@
package mc.core.events.runner; package mc.core.events.runner;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.locks.Lock;
public interface ResourceRunnable extends Runnable { public interface ResourceRunnable extends Runnable {
default void lock() { default List<Lock> getLocks() {
return Collections.emptyList();
}
default void unlock() {
} }
default void after() { default void after() {

View File

@@ -1,7 +1,6 @@
package mc.core.events; package mc.core.events;
import mc.core.events.runner.EventExecutorService; import mc.core.events.runner.EventExecutorService;
import mc.core.events.runner.ResourceRunnable;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@@ -17,22 +16,9 @@ public class EventExecutorTest {
CountDownLatch latch = new CountDownLatch(1); CountDownLatch latch = new CountDownLatch(1);
EventExecutorService service = new EventExecutorService(1); EventExecutorService service = new EventExecutorService(1);
service.start(); service.start();
service.addTask(new ResourceRunnable() { service.addTask(() -> {
@Override
public void lock() {
}
@Override
public void unlock() {
}
@Override
public void run() {
testVariable.set(true); testVariable.set(true);
latch.countDown(); latch.countDown();
}
}); });
latch.await(1, TimeUnit.SECONDS); latch.await(1, TimeUnit.SECONDS);