From 73c88dd3654c8e46b03d4edc8e8437621ed3aef1 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Tue, 4 Nov 2014 11:11:04 +0000 Subject: [PATCH] * make sure core size is not limited to max thread pool size (seems to be a enforced limit now in JDK 8) --- .../ui/sfv/ChecksumComputationService.java | 67 +++++++------------ 1 file changed, 24 insertions(+), 43 deletions(-) diff --git a/source/net/filebot/ui/sfv/ChecksumComputationService.java b/source/net/filebot/ui/sfv/ChecksumComputationService.java index 0040092b..7ff1cbbd 100644 --- a/source/net/filebot/ui/sfv/ChecksumComputationService.java +++ b/source/net/filebot/ui/sfv/ChecksumComputationService.java @@ -1,7 +1,5 @@ - package net.filebot.ui.sfv; - import static java.lang.Math.*; import java.beans.PropertyChangeListener; @@ -17,21 +15,18 @@ import java.util.concurrent.atomic.AtomicInteger; import net.filebot.util.DefaultThreadFactory; - class ChecksumComputationService { - + public static final String TASK_COUNT_PROPERTY = "taskCount"; - + private final Set executors = new HashSet(4); - + private final AtomicInteger completedTaskCount = new AtomicInteger(0); private final AtomicInteger totalTaskCount = new AtomicInteger(0); - - + public ExecutorService newExecutor() { return new ChecksumComputationExecutor(); } - public void reset() { synchronized (executors) { @@ -43,16 +38,15 @@ class ChecksumComputationService { } } } - + totalTaskCount.set(0); completedTaskCount.set(0); - + executors.clear(); } - + pcs.firePropertyChange(TASK_COUNT_PROPERTY, -1, getTaskCount()); } - /** * Get the number of active executors that are associated with this {@link ChecksumComputationService}. @@ -65,22 +59,18 @@ class ChecksumComputationService { return executors.size(); } } - public int getTaskCount() { return totalTaskCount.get() - completedTaskCount.get(); } - public int getTotalTaskCount() { return totalTaskCount.get(); } - public int getCompletedTaskCount() { return completedTaskCount.get(); } - public void purge() { synchronized (executors) { @@ -89,13 +79,12 @@ class ChecksumComputationService { } } } - - + private class ChecksumComputationExecutor extends ThreadPoolExecutor { - + public ChecksumComputationExecutor() { - super(1, 1, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue(), new DefaultThreadFactory("ChecksumComputationPool", Thread.MIN_PRIORITY)); - + super(1, Runtime.getRuntime().availableProcessors(), 0L, TimeUnit.SECONDS, new LinkedBlockingQueue(), new DefaultThreadFactory("ChecksumComputationPool", Thread.MIN_PRIORITY)); + synchronized (executors) { if (executors.add(this) && executors.size() == 1) { // first executor of a new session, reset counts @@ -103,10 +92,9 @@ class ChecksumComputationService { completedTaskCount.set(0); } } - + prestartAllCoreThreads(); } - protected int getPreferredPoolSize() { // for a few files, use one thread @@ -114,67 +102,62 @@ class ChecksumComputationService { // e.g 50 files ~ 1 thread, 200 files ~ 2 threads, 1000 files ~ 3 threads, 40000 files ~ 5 threads return max((int) log10(getQueue().size()), 1); } - @Override public void execute(Runnable command) { int preferredPoolSize = getPreferredPoolSize(); - + if (getCorePoolSize() < preferredPoolSize) { setCorePoolSize(preferredPoolSize); } - + synchronized (this) { super.execute(command); } - + totalTaskCount.incrementAndGet(); - + pcs.firePropertyChange(TASK_COUNT_PROPERTY, getTaskCount() - 1, getTaskCount()); } - @Override public void purge() { int delta = 0; - + synchronized (this) { delta += getQueue().size(); super.purge(); delta -= getQueue().size(); } - + if (delta > 0) { // subtract removed tasks from task count totalTaskCount.addAndGet(-delta); - + pcs.firePropertyChange(TASK_COUNT_PROPERTY, getTaskCount() + delta, getTaskCount()); } } - @Override protected void afterExecute(Runnable r, Throwable t) { super.afterExecute(r, t); - + if (isValid()) { if (r instanceof Future && ((Future) r).isCancelled()) { totalTaskCount.decrementAndGet(); } else { completedTaskCount.incrementAndGet(); } - + pcs.firePropertyChange(TASK_COUNT_PROPERTY, getTaskCount() + 1, getTaskCount()); } } - protected boolean isValid() { synchronized (executors) { return executors.contains(this); } } - @Override protected void terminated() { @@ -183,17 +166,15 @@ class ChecksumComputationService { } } } - + private final PropertyChangeSupport pcs = new PropertyChangeSupport(this); - - + public void addPropertyChangeListener(PropertyChangeListener listener) { pcs.addPropertyChangeListener(listener); } - public void removePropertyChangeListener(PropertyChangeListener listener) { pcs.removePropertyChangeListener(listener); } - + }