* make sure core size is not limited to max thread pool size (seems to be a enforced limit now in JDK 8)

This commit is contained in:
Reinhard Pointner 2014-11-04 11:11:04 +00:00
parent fc70050ce3
commit 73c88dd365
1 changed files with 24 additions and 43 deletions

View File

@ -1,7 +1,5 @@
package net.filebot.ui.sfv;
import static java.lang.Math.*;
import java.beans.PropertyChangeListener;
@ -17,7 +15,6 @@ import java.util.concurrent.atomic.AtomicInteger;
import net.filebot.util.DefaultThreadFactory;
class ChecksumComputationService {
public static final String TASK_COUNT_PROPERTY = "taskCount";
@ -27,12 +24,10 @@ class ChecksumComputationService {
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) {
for (ExecutorService executor : executors) {
@ -53,7 +48,6 @@ class ChecksumComputationService {
pcs.firePropertyChange(TASK_COUNT_PROPERTY, -1, getTaskCount());
}
/**
* Get the number of active executors that are associated with this {@link ChecksumComputationService}.
*
@ -66,22 +60,18 @@ class ChecksumComputationService {
}
}
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) {
for (ThreadPoolExecutor executor : executors) {
@ -90,11 +80,10 @@ class ChecksumComputationService {
}
}
private class ChecksumComputationExecutor extends ThreadPoolExecutor {
public ChecksumComputationExecutor() {
super(1, 1, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new DefaultThreadFactory("ChecksumComputationPool", Thread.MIN_PRIORITY));
super(1, Runtime.getRuntime().availableProcessors(), 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new DefaultThreadFactory("ChecksumComputationPool", Thread.MIN_PRIORITY));
synchronized (executors) {
if (executors.add(this) && executors.size() == 1) {
@ -107,7 +96,6 @@ class ChecksumComputationService {
prestartAllCoreThreads();
}
protected int getPreferredPoolSize() {
// for a few files, use one thread
// for lots of files, use multiple threads
@ -115,7 +103,6 @@ class ChecksumComputationService {
return max((int) log10(getQueue().size()), 1);
}
@Override
public void execute(Runnable command) {
int preferredPoolSize = getPreferredPoolSize();
@ -133,7 +120,6 @@ class ChecksumComputationService {
pcs.firePropertyChange(TASK_COUNT_PROPERTY, getTaskCount() - 1, getTaskCount());
}
@Override
public void purge() {
int delta = 0;
@ -152,7 +138,6 @@ class ChecksumComputationService {
}
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
@ -168,14 +153,12 @@ class ChecksumComputationService {
}
}
protected boolean isValid() {
synchronized (executors) {
return executors.contains(this);
}
}
@Override
protected void terminated() {
synchronized (executors) {
@ -186,12 +169,10 @@ 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);
}