Class ObjectBoxThreadPoolExecutor
- All Implemented Interfaces:
Executor,ExecutorService
ThreadPoolExecutor that automatically releases thread-local ObjectBox resources after each task execution
by calling BoxStore.closeThreadResources().
This is useful when using a thread pool with ObjectBox to ensure that thread-local resources (currently readers only) are properly cleaned up after each task completes.
Recommended: Use the factory methods BoxStore.newFixedThreadPoolExecutor(int) or
BoxStore.newCachedThreadPoolExecutor() to create instances of this executor.
Example usage:
BoxStore boxStore = MyObjectBox.builder().build();
// Recommended: Use BoxStore factory methods
ObjectBoxThreadPoolExecutor executor = boxStore.newFixedThreadPoolExecutor(4);
// Or for a cached thread pool
ObjectBoxThreadPoolExecutor cachedExecutor = boxStore.newCachedThreadPoolExecutor();
// Advanced: Direct construction for custom configuration
ObjectBoxThreadPoolExecutor customExecutor = new ObjectBoxThreadPoolExecutor(
boxStore,
4, // core pool size
8, // maximum pool size
60L, TimeUnit.SECONDS, // keep-alive time
new LinkedBlockingQueue<>()
);
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.concurrent.ThreadPoolExecutor
ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy -
Constructor Summary
ConstructorsConstructorDescriptionObjectBoxThreadPoolExecutor(BoxStore boxStore, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) Creates a new ObjectBoxThreadPoolExecutor with the given parameters.ObjectBoxThreadPoolExecutor(BoxStore boxStore, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) Creates a new ObjectBoxThreadPoolExecutor with the given parameters.ObjectBoxThreadPoolExecutor(BoxStore boxStore, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) Creates a new ObjectBoxThreadPoolExecutor with the given parameters.ObjectBoxThreadPoolExecutor(BoxStore boxStore, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) Creates a new ObjectBoxThreadPoolExecutor with the given parameters. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidafterExecute(Runnable runnable, Throwable throwable) Releases thread-local ObjectBox resources after each task execution.Methods inherited from class java.util.concurrent.ThreadPoolExecutor
allowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, beforeExecute, execute, finalize, getActiveCount, getCompletedTaskCount, getCorePoolSize, getKeepAliveTime, getLargestPoolSize, getMaximumPoolSize, getPoolSize, getQueue, getRejectedExecutionHandler, getTaskCount, getThreadFactory, isShutdown, isTerminated, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, remove, setCorePoolSize, setKeepAliveTime, setMaximumPoolSize, setRejectedExecutionHandler, setThreadFactory, shutdown, shutdownNow, terminated, toStringMethods inherited from class java.util.concurrent.AbstractExecutorService
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submit
-
Constructor Details
-
ObjectBoxThreadPoolExecutor
public ObjectBoxThreadPoolExecutor(BoxStore boxStore, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) Creates a new ObjectBoxThreadPoolExecutor with the given parameters.See
ThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue)for parameter details.- Parameters:
boxStore- the BoxStore instance for which to close thread resources
-
ObjectBoxThreadPoolExecutor
public ObjectBoxThreadPoolExecutor(BoxStore boxStore, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) Creates a new ObjectBoxThreadPoolExecutor with the given parameters.See
ThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue, ThreadFactory)for parameter details.- Parameters:
boxStore- the BoxStore instance for which to close thread resources
-
ObjectBoxThreadPoolExecutor
public ObjectBoxThreadPoolExecutor(BoxStore boxStore, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) Creates a new ObjectBoxThreadPoolExecutor with the given parameters.See
ThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue, RejectedExecutionHandler)for parameter details.- Parameters:
boxStore- the BoxStore instance for which to close thread resources
-
ObjectBoxThreadPoolExecutor
public ObjectBoxThreadPoolExecutor(BoxStore boxStore, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) Creates a new ObjectBoxThreadPoolExecutor with the given parameters.See
ThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue, ThreadFactory, RejectedExecutionHandler)for parameter details.- Parameters:
boxStore- the BoxStore instance for which to close thread resources
-
-
Method Details
-
afterExecute
Releases thread-local ObjectBox resources after each task execution.- Overrides:
afterExecutein classThreadPoolExecutor
-