Skip to content

Instantly share code, notes, and snippets.

@greenrobot
Created August 28, 2019 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greenrobot/6a1e818193c8b2d0a661dca5441ab94b to your computer and use it in GitHub Desktop.
Save greenrobot/6a1e818193c8b2d0a661dca5441ab94b to your computer and use it in GitHub Desktop.
ObjectBox example: Cache query and multi-threaded usage of cached query
package io.objectbox.test.stress;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import io.objectbox.Box;
import io.objectbox.BoxStore;
import io.objectbox.query.Query;
public class Stresser {
static final boolean VERBOSE = false;
private final BoxStore store;
private final Query<TempData> findFirstQuery;
ThreadPoolExecutor pool;
public Stresser(BoxStore store) {
this.store = store;
findFirstQuery = store.boxFor(TempData.class).query().equal(TempData_.longVal, 0L).build();
// Originally at 128 threads, decrease to go well with default value of readers
pool = new ThreadPoolExecutor(5, 100, 7L, TimeUnit.SECONDS, new OfferLinkedBlockingQueue());
}
protected void runFindFirstQueries() {
for (int i = 0; i < 2000; i++) {
final int j = i;
Runnable r = new Runnable() {
@Override
public void run() {
TempData data;
synchronized (findFirstQuery) {
data = findFirstQuery.setParameter(TempData_.longVal, (long) j).findFirst();
}
if (VERBOSE) System.out.println(data);
}
};
pool.execute(r);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment