markt-asf commented on a change in pull request #32:
URL: https://github.com/apache/commons-pool/pull/32#discussion_r458282816
##########
File path: src/main/java/org/apache/commons/pool2/impl/EvictionTimer.java
##########
@@ -129,4 +167,45 @@ public Thread newThread(final Runnable runnable) {
return thread;
}
}
+
+ /**
+ * Task that removes references to abandoned tasks and shuts
+ * down the executor if there are no live tasks left.
+ */
+ private static class Reaper implements Runnable {
+ @Override
+ public void run() {
+ synchronized (EvictionTimer.class) {
+ for (Entry<WeakReference<Runnable>, WeakRunner> entry :
taskMap.entrySet()) {
+ if (entry.getKey().get() == null) {
+ executor.remove(entry.getValue());
+ taskMap.remove(entry.getKey());
+ }
+ }
Review comment:
This loop may be unnecessary (see comment on WeakRunner below).
##########
File path: src/main/java/org/apache/commons/pool2/impl/EvictionTimer.java
##########
@@ -79,10 +86,15 @@ static synchronized void schedule(
if (null == executor) {
executor = new ScheduledThreadPoolExecutor(1, new
EvictorThreadFactory());
executor.setRemoveOnCancelPolicy(true);
+ taskMap.clear();
Review comment:
This line should unnecessary shouldn't it?
##########
File path: src/main/java/org/apache/commons/pool2/impl/EvictionTimer.java
##########
@@ -79,10 +86,15 @@ static synchronized void schedule(
if (null == executor) {
executor = new ScheduledThreadPoolExecutor(1, new
EvictorThreadFactory());
executor.setRemoveOnCancelPolicy(true);
+ taskMap.clear();
+ executor.scheduleAtFixedRate(new Reaper(), delay, period,
TimeUnit.MILLISECONDS);
}
+ final WeakReference<Runnable> ref = new WeakReference<>(task);
+ final WeakRunner runner = new WeakRunner(ref);
Review comment:
Maybe create the WeakReference in WeakRunner's constructor?
##########
File path:
src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java
##########
@@ -783,12 +783,21 @@ final void assertOpen() throws IllegalStateException {
*/
final void startEvictor(final long delay) {
synchronized (evictionLock) {
- EvictionTimer.cancel(evictor, evictorShutdownTimeoutMillis,
TimeUnit.MILLISECONDS);
- evictor = null;
- evictionIterator = null;
- if (delay > 0) {
- evictor = new Evictor();
- EvictionTimer.schedule(evictor, delay, delay);
+ if (evictor == null) { // Starting evictor for the first time or
after a cancel
+ if (delay > 0) { // Starting new evictor
+ evictor = new Evictor();
+ EvictionTimer.schedule(evictor, delay, delay);
+ }
+ } else { // Stop or restart of existing evictor
+ if (delay > 0) { // Restart
+ EvictionTimer.cancel(evictor,
evictorShutdownTimeoutMillis, TimeUnit.MILLISECONDS, true);
+ evictor = null;
+ evictionIterator = null;
+ evictor = new Evictor();
+ EvictionTimer.schedule(evictor, delay, delay);
+ } else { // Stopping evictor
Review comment:
There is a narrow window where one Evictor restarting and a second being
cancelled could trigger the EvictionTimer to shutdown its underlying Executor
but I'm not sure the refactoring necessary to fix that is worth it.
##########
File path: src/main/java/org/apache/commons/pool2/impl/EvictionTimer.java
##########
@@ -129,4 +167,45 @@ public Thread newThread(final Runnable runnable) {
return thread;
}
}
+
+ /**
+ * Task that removes references to abandoned tasks and shuts
+ * down the executor if there are no live tasks left.
+ */
+ private static class Reaper implements Runnable {
+ @Override
+ public void run() {
+ synchronized (EvictionTimer.class) {
+ for (Entry<WeakReference<Runnable>, WeakRunner> entry :
taskMap.entrySet()) {
+ if (entry.getKey().get() == null) {
+ executor.remove(entry.getValue());
+ taskMap.remove(entry.getKey());
+ }
+ }
+ if (taskMap.isEmpty() && executor != null) {
+ executor.shutdown();
+ executor.setCorePoolSize(0);
+ executor = null;
+ }
+ }
+ }
+ }
+
+ /**
+ * Runnable that runs the referent of a weak reference. When the referent
is no
+ * no longer reachable, run is no-op.
+ */
+ private static class WeakRunner implements Runnable {
+ private final WeakReference<Runnable> ref;
+ public WeakRunner(WeakReference<Runnable> ref) {
+ this.ref = ref;
+ }
+ @Override
+ public void run() {
+ final Runnable task = ref.get();
+ if (task != null) {
+ task.run();
+ }
+ }
Review comment:
It would be cleaner to cancel the WeakRunner here but if that isn't
possible consider adding a comment to that effect to aid future maintenance.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]