
goal.tools.history.EventStorageWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of runtime Show documentation
Show all versions of runtime Show documentation
A system for running GOAL multi-agent systems.
The newest version!
package goal.tools.history;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.mapdb.IndexTreeList;
import goal.tools.history.events.AbstractEvent;
public class EventStorageWriter implements Runnable {
private final static ThreadPoolExecutor pool;
static {
pool = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 1L, TimeUnit.SECONDS, new SynchronousQueue());
pool.setThreadFactory(new ThreadFactory() {
@Override
public Thread newThread(final Runnable r) {
final Thread t = new Thread(r);
t.setPriority(Thread.MIN_PRIORITY); // 1 less than the agent threads
return t;
}
});
}
private final IndexTreeList storage;
private volatile BlockingQueue queue;
public EventStorageWriter(final IndexTreeList storage) {
this.storage = storage;
this.queue = new LinkedBlockingQueue<>();
pool.submit(this);
}
public void write(final AbstractEvent value) {
this.queue.add(value);
}
public void finish() throws InterruptedException {
while (this.queue != null && !this.queue.isEmpty()) {
synchronized (this) {
wait();
}
}
this.queue = null;
}
@Override
public void run() {
while (this.queue != null) {
try {
if (this.queue.drainTo(this.storage) > 0) {
this.storage.getStore().commit();
synchronized (this) {
notifyAll();
}
}
Thread.sleep(1);
} catch (final Throwable e) {
e.printStackTrace(); // FIXME
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy