Функции start/stop

This commit is contained in:
Gravit 2018-10-18 21:46:14 +07:00
parent fc6a29cfa2
commit 40392f24cd
No known key found for this signature in database
GPG key ID: 061981E1E85D3216

View file

@ -1,5 +1,6 @@
package ru.gravit.utils.event; package ru.gravit.utils.event;
import ru.gravit.utils.helper.CommonHelper;
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
import java.util.ArrayList; import java.util.ArrayList;
@ -7,6 +8,7 @@
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
public class EventManager { public class EventManager {
public static final int QUEUE_MAX_SIZE = 2048; public static final int QUEUE_MAX_SIZE = 2048;
@ -31,6 +33,26 @@ public QueueEntry(EventInterface event, UUID key) {
EventInterface event; EventInterface event;
UUID key; UUID key;
} }
private EventExecutor executor;
private Thread executorThread;
private AtomicBoolean isStarted = new AtomicBoolean(false);
public synchronized void start()
{
if(isStarted.get()) return;
executor = new EventExecutor();
isStarted.set(true);
executorThread = CommonHelper.newThread("EventExecutor",true,executor);
executorThread.start();
}
public synchronized void stop()
{
if(!isStarted.get()) return;
executorThread.interrupt();
try {
executorThread.join();
} catch (InterruptedException ignored) {
}
}
public ArrayList<Entry> handlers = new ArrayList<>(INITIAL_HANDLERS_SIZE); public ArrayList<Entry> handlers = new ArrayList<>(INITIAL_HANDLERS_SIZE);
public BlockingQueue<QueueEntry> queue = new LinkedBlockingQueue<>(QUEUE_MAX_SIZE); //Максимальный размер очереди public BlockingQueue<QueueEntry> queue = new LinkedBlockingQueue<>(QUEUE_MAX_SIZE); //Максимальный размер очереди
public int registerHandler(EventHandler<EventInterface> func, UUID[] events) public int registerHandler(EventHandler<EventInterface> func, UUID[] events)
@ -39,6 +61,10 @@ public int registerHandler(EventHandler<EventInterface> func, UUID[] events)
handlers.add(new Entry(func,events)); handlers.add(new Entry(func,events));
return handlers.size(); return handlers.size();
} }
public void unregisterHandler(EventHandler<EventInterface> func)
{
handlers.removeIf(e -> e.func.equals(func));
}
public void sendEvent(UUID key, EventInterface event, boolean blocking) public void sendEvent(UUID key, EventInterface event, boolean blocking)
{ {
if(blocking) process(key,event); if(blocking) process(key,event);