Created
August 28, 2019 09:58
-
-
Save greenrobot/6a1e818193c8b2d0a661dca5441ab94b to your computer and use it in GitHub Desktop.
ObjectBox example: Cache query and multi-threaded usage of cached query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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