|
17 | 17 | package io.objectbox.internal; |
18 | 18 |
|
19 | 19 | import java.util.concurrent.Executors; |
20 | | -import java.util.concurrent.SynchronousQueue; |
21 | | -import java.util.concurrent.ThreadFactory; |
22 | | -import java.util.concurrent.ThreadPoolExecutor; |
| 20 | +import java.util.List; |
| 21 | +import java.util.concurrent.AbstractExecutorService; |
| 22 | +import java.util.concurrent.ExecutorService; |
| 23 | +import java.util.concurrent.ForkJoinPool; |
| 24 | +import java.util.concurrent.ForkJoinWorkerThread; |
23 | 25 | import java.util.concurrent.TimeUnit; |
24 | | -import java.util.concurrent.atomic.AtomicInteger; |
25 | 26 |
|
26 | 27 | import io.objectbox.BoxStore; |
27 | 28 | import io.objectbox.annotation.apihint.Internal; |
28 | 29 |
|
29 | 30 | /** |
30 | | - * Custom thread pool similar to {@link Executors#newCachedThreadPool()} with the following adjustments: |
| 31 | + * Custom executor service similar to {@link Executors#newWorkStealingPool()} with the following adjustments: |
31 | 32 | * <ul> |
32 | | - * <li>Release thread local resources ({@link BoxStore#closeThreadResources()})</li> |
33 | | - * <li>Reduce keep-alive time for threads to 20 seconds</li> |
34 | | - * <li>Uses a ThreadFactory to name threads like "ObjectBox-1-Thread-1"</li> |
| 33 | + * <li>Release thread local resources ({@link BoxStore#closeThreadResources()}) after task execution</li> |
| 34 | + * <li>Uses a custom thread factory to name threads like "ObjectBox-ForkJoinPool-1-Thread-1"</li> |
35 | 35 | * </ul> |
36 | 36 | * |
37 | 37 | */ |
38 | 38 | @Internal |
39 | | -public class ObjectBoxThreadPool extends ThreadPoolExecutor { |
| 39 | +public final class ObjectBoxThreadPool extends AbstractExecutorService { |
40 | 40 | private final BoxStore boxStore; |
| 41 | + private final ExecutorService executorImpl; |
41 | 42 |
|
42 | | - public ObjectBoxThreadPool(BoxStore boxStore) { |
43 | | - super(0, Integer.MAX_VALUE, 20L, TimeUnit.SECONDS, new SynchronousQueue<>(), |
44 | | - new ObjectBoxThreadFactory()); |
| 43 | + public ObjectBoxThreadPool(BoxStore boxStore, int parallelism) { |
45 | 44 | this.boxStore = boxStore; |
| 45 | + this.executorImpl = Executors.unconfigurableExecutorService( |
| 46 | + new ForkJoinPool( |
| 47 | + parallelism, |
| 48 | + pool -> { |
| 49 | + ForkJoinWorkerThread thread = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool); |
| 50 | + // Priority and daemon status are inherited from calling thread; ensure to reset if required |
| 51 | + if (thread.getPriority() != Thread.NORM_PRIORITY) { |
| 52 | + thread.setPriority(Thread.NORM_PRIORITY); |
| 53 | + } |
| 54 | + if (thread.isDaemon()) { |
| 55 | + thread.setDaemon(false); |
| 56 | + } |
| 57 | + thread.setName("ObjectBox-" + thread.getName()); |
| 58 | + return thread; |
| 59 | + }, |
| 60 | + null, |
| 61 | + false)); |
46 | 62 | } |
47 | 63 |
|
| 64 | + |
48 | 65 | @Override |
49 | | - protected void afterExecute(Runnable runnable, Throwable throwable) { |
50 | | - super.afterExecute(runnable, throwable); |
51 | | - boxStore.closeThreadResources(); |
| 66 | + public void shutdown() { |
| 67 | + executorImpl.shutdown(); |
52 | 68 | } |
53 | 69 |
|
54 | | - static class ObjectBoxThreadFactory implements ThreadFactory { |
55 | | - private static final AtomicInteger POOL_COUNT = new AtomicInteger(); |
| 70 | + @Override |
| 71 | + public List<Runnable> shutdownNow() { |
| 72 | + return executorImpl.shutdownNow(); |
| 73 | + } |
56 | 74 |
|
57 | | - private final ThreadGroup group; |
58 | | - private final String namePrefix = "ObjectBox-" + POOL_COUNT.incrementAndGet() + "-Thread-"; |
59 | | - private final AtomicInteger threadCount = new AtomicInteger(); |
| 75 | + @Override |
| 76 | + public boolean isShutdown() { |
| 77 | + return executorImpl.isShutdown(); |
| 78 | + } |
60 | 79 |
|
61 | | - ObjectBoxThreadFactory() { |
62 | | - SecurityManager securityManager = System.getSecurityManager(); |
63 | | - group = (securityManager != null) ? securityManager.getThreadGroup() : |
64 | | - Thread.currentThread().getThreadGroup(); |
65 | | - } |
| 80 | + @Override |
| 81 | + public boolean isTerminated() { |
| 82 | + return executorImpl.isTerminated(); |
| 83 | + } |
66 | 84 |
|
67 | | - public Thread newThread(Runnable runnable) { |
68 | | - String name = namePrefix + threadCount.incrementAndGet(); |
69 | | - Thread thread = new Thread(group, runnable, name); |
| 85 | + @Override |
| 86 | + public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { |
| 87 | + return executorImpl.awaitTermination(timeout, unit); |
| 88 | + } |
70 | 89 |
|
71 | | - // Priority and daemon status are inherited from calling thread; ensure to reset if required |
72 | | - if (thread.getPriority() != Thread.NORM_PRIORITY) { |
73 | | - thread.setPriority(Thread.NORM_PRIORITY); |
74 | | - } |
75 | | - if (thread.isDaemon()) { |
76 | | - thread.setDaemon(false); |
| 90 | + @Override |
| 91 | + public void execute(Runnable command) { |
| 92 | + executorImpl.execute(() -> { |
| 93 | + try { |
| 94 | + command.run(); |
| 95 | + } finally { |
| 96 | + boxStore.closeThreadResources(); |
77 | 97 | } |
78 | | - return thread; |
79 | | - } |
| 98 | + }); |
80 | 99 | } |
81 | 100 | } |
0 commit comments