Skip to content

Commit df3f207

Browse files
committed
Builder API to configure thread pool size.
1 parent 7d5f6ca commit df3f207

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

objectbox-java/src/main/java/io/objectbox/BoxStore.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public static boolean isSyncServerAvailable() {
227227
private final int[] allEntityTypeIds;
228228
private final Map<Class<?>, Box<?>> boxes = new ConcurrentHashMap<>();
229229
private final Set<Transaction> transactions = Collections.newSetFromMap(new WeakHashMap<>());
230-
private final ExecutorService threadPool = new ObjectBoxThreadPool(this);
230+
private final ExecutorService threadPool;
231231
private final ObjectClassPublisher objectClassPublisher;
232232
final boolean debugTxRead;
233233
final boolean debugTxWrite;
@@ -257,6 +257,11 @@ public static boolean isSyncServerAvailable() {
257257
private SyncClient syncClient;
258258

259259
BoxStore(BoxStoreBuilder builder) {
260+
threadPool = new ObjectBoxThreadPool(
261+
this,
262+
builder.threadPoolCoreSize,
263+
builder.threadPoolMaxSize,
264+
builder.threadPoolThreadKeepAliveSeconds);
260265
context = builder.context;
261266
relinker = builder.relinker;
262267
NativeLibraryLoader.ensureLoaded();

objectbox-java/src/main/java/io/objectbox/BoxStoreBuilder.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ public class BoxStoreBuilder {
9191

9292
int fileMode;
9393

94+
int threadPoolCoreSize = 0;
95+
96+
int threadPoolMaxSize = Integer.MAX_VALUE;
97+
98+
int threadPoolThreadKeepAliveSeconds = 20;
99+
94100
int maxReaders;
95101
boolean noReaderThreadLocals;
96102

@@ -288,6 +294,35 @@ public BoxStoreBuilder fileMode(int mode) {
288294
return this;
289295
}
290296

297+
/**
298+
* Sets the core size of thread pool used by BoxStore. The default is 0.
299+
* You may want to change it along with {@link #threadPoolThreadKeepAliveSeconds}
300+
* to make fresh threads creation not very often.
301+
*/
302+
public BoxStoreBuilder threadPoolCoreSize(int threadPoolCoreSize) {
303+
this.threadPoolCoreSize = threadPoolCoreSize;
304+
return this;
305+
}
306+
307+
/**
308+
* Sets the max number of threads allowed in thread pool used by BoxStore.
309+
* The default is {@link Integer#MAX_VALUE}.
310+
* You may want to limit max thread pool size to avoid OOM when thread
311+
* count become too large for device.
312+
*/
313+
public BoxStoreBuilder threadPoolMaxSize(int threadPoolMaxSize) {
314+
this.threadPoolMaxSize = threadPoolMaxSize;
315+
return this;
316+
}
317+
318+
/**
319+
* Sets the number of seconds thread kan stay idle in thread pool before termination.
320+
*/
321+
public BoxStoreBuilder threadPoolThreadKeepAliveSeconds(int threadPoolThreadKeepAliveSeconds) {
322+
this.threadPoolThreadKeepAliveSeconds = threadPoolThreadKeepAliveSeconds;
323+
return this;
324+
}
325+
291326
/**
292327
* Sets the maximum number of concurrent readers. For most applications, the default is fine (~ 126 readers).
293328
* <p>

objectbox-java/src/main/java/io/objectbox/internal/ObjectBoxThreadPool.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
public class ObjectBoxThreadPool extends ThreadPoolExecutor {
4040
private final BoxStore boxStore;
4141

42-
public ObjectBoxThreadPool(BoxStore boxStore) {
43-
super(0, Integer.MAX_VALUE, 20L, TimeUnit.SECONDS, new SynchronousQueue<>(),
42+
public ObjectBoxThreadPool(BoxStore boxStore, int corePoolSize, int maximumPoolSize, int threadKeepAliveSeconds) {
43+
super(corePoolSize, maximumPoolSize, threadKeepAliveSeconds, TimeUnit.SECONDS, new SynchronousQueue<>(),
4444
new ObjectBoxThreadFactory());
4545
this.boxStore = boxStore;
4646
}

0 commit comments

Comments
 (0)