commit 92b0ab2fbc7d40e53681d489fca98419a3547f56
Author: Wiktor Niesiobędzki <github@vink.pl>
Date: Fri Sep 14 23:39:03 2018 +0200
Remove setCorePoolSize hack and instead use
ThreadPool.DiscardOldestPolicy
diff --git src/org/openstreetmap/josm/data/cache/HostLimitQueue.java src/org/openstreetmap/josm/data/cache/HostLimitQueue.java
index 9f38c1d7b..aa4a397b3 100644
|
|
|
import java.util.Map;
|
| 8 | 8 | import java.util.concurrent.ConcurrentHashMap; |
| 9 | 9 | import java.util.concurrent.LinkedBlockingDeque; |
| 10 | 10 | import java.util.concurrent.Semaphore; |
| 11 | | import java.util.concurrent.ThreadPoolExecutor; |
| 12 | 11 | import java.util.concurrent.TimeUnit; |
| 13 | 12 | |
| 14 | 13 | import org.openstreetmap.josm.tools.Logging; |
| … |
… |
public class HostLimitQueue extends LinkedBlockingDeque<Runnable> {
|
| 33 | 32 | private final Map<String, Semaphore> hostSemaphores = new ConcurrentHashMap<>(); |
| 34 | 33 | private final int hostLimit; |
| 35 | 34 | |
| 36 | | private ThreadPoolExecutor executor; |
| 37 | | |
| 38 | | private int corePoolSize; |
| 39 | | |
| 40 | | private int maximumPoolSize; |
| 41 | | |
| 42 | 35 | /** |
| 43 | 36 | * Creates an unbounded queue |
| 44 | 37 | * @param hostLimit how many parallel calls to host to allow |
| … |
… |
public class HostLimitQueue extends LinkedBlockingDeque<Runnable> {
|
| 48 | 41 | this.hostLimit = hostLimit; |
| 49 | 42 | } |
| 50 | 43 | |
| | 44 | public HostLimitQueue(int hostLimit, int queueLimit) { |
| | 45 | super(queueLimit); // create unbounded queue |
| | 46 | this.hostLimit = hostLimit; |
| | 47 | } |
| | 48 | |
| 51 | 49 | private JCSCachedTileLoaderJob<?, ?> findJob() { |
| 52 | 50 | for (Iterator<Runnable> it = iterator(); it.hasNext();) { |
| 53 | 51 | Runnable r = it.next(); |
| … |
… |
public class HostLimitQueue extends LinkedBlockingDeque<Runnable> {
|
| 116 | 114 | return job; |
| 117 | 115 | } |
| 118 | 116 | |
| 119 | | /** |
| 120 | | * Set the executor for which this queue works. It's needed to spawn new threads. |
| 121 | | * See: http://stackoverflow.com/questions/9622599/java-threadpoolexecutor-strategy-direct-handoff-with-queue# |
| 122 | | * |
| 123 | | * @param executor executor for which this queue works |
| 124 | | */ |
| 125 | | public void setExecutor(ThreadPoolExecutor executor) { |
| 126 | | this.executor = executor; |
| 127 | | this.maximumPoolSize = executor.getMaximumPoolSize(); |
| 128 | | this.corePoolSize = executor.getCorePoolSize(); |
| 129 | | } |
| 130 | | |
| 131 | | @Override |
| 132 | | public boolean offer(Runnable e) { |
| 133 | | if (!super.offer(e)) { |
| 134 | | return false; |
| 135 | | } |
| 136 | | |
| 137 | | if (executor != null) { |
| 138 | | // See: http://stackoverflow.com/questions/9622599/java-threadpoolexecutor-strategy-direct-handoff-with-queue# |
| 139 | | // force spawn of a thread if not reached maximum |
| 140 | | int currentPoolSize = executor.getPoolSize(); |
| 141 | | if (currentPoolSize < maximumPoolSize |
| 142 | | && currentPoolSize >= corePoolSize) { |
| 143 | | executor.setCorePoolSize(currentPoolSize + 1); |
| 144 | | executor.setCorePoolSize(corePoolSize); |
| 145 | | } |
| 146 | | } |
| 147 | | return true; |
| 148 | | } |
| 149 | | |
| 150 | 117 | private Semaphore getSemaphore(JCSCachedTileLoaderJob<?, ?> job) { |
| 151 | 118 | String host; |
| 152 | 119 | try { |
diff --git src/org/openstreetmap/josm/data/imagery/TMSCachedTileLoader.java src/org/openstreetmap/josm/data/imagery/TMSCachedTileLoader.java
index a1e566aa4..03aec0dc4 100644
|
|
|
public class TMSCachedTileLoader implements TileLoader, CachedTileLoader {
|
| 67 | 67 | * @return new ThreadPoolExecutor that will use a @see HostLimitQueue based queue |
| 68 | 68 | */ |
| 69 | 69 | public static ThreadPoolExecutor getNewThreadPoolExecutor(String nameFormat, int workers) { |
| 70 | | HostLimitQueue workQueue = new HostLimitQueue(HOST_LIMIT.get().intValue()); |
| 71 | 70 | ThreadPoolExecutor executor = new ThreadPoolExecutor( |
| 72 | | 0, // 0 so for unused thread pools threads will eventually die, freeing also the threadpool |
| 73 | | workers, // do not this number of threads |
| | 71 | 0, // keep core pool the same size as max |
| | 72 | workers, // do not exceed this number of threads |
| 74 | 73 | 300, // keepalive for thread |
| 75 | 74 | TimeUnit.SECONDS, |
| 76 | | workQueue, |
| | 75 | new HostLimitQueue(HOST_LIMIT.get().intValue(), workers), |
| 77 | 76 | Utils.newThreadFactory(nameFormat, Thread.NORM_PRIORITY) |
| 78 | 77 | ); |
| 79 | | workQueue.setExecutor(executor); |
| | 78 | executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy()); |
| 80 | 79 | return executor; |
| 81 | 80 | } |
| 82 | 81 | |
diff --git test/unit/org/openstreetmap/josm/data/cache/HostLimitQueueTest.java test/unit/org/openstreetmap/josm/data/cache/HostLimitQueueTest.java
index 36e7cde59..f3c6615e4 100644
|
|
|
public class HostLimitQueueTest {
|
| 33 | 33 | public JOSMTestRules test = new JOSMTestRules().preferences().timeout(20 * 1000); |
| 34 | 34 | |
| 35 | 35 | private static ThreadPoolExecutor getNewThreadPoolExecutor(String nameFormat, int workers, int queueLimit) { |
| 36 | | HostLimitQueue workQueue = new HostLimitQueue(queueLimit); |
| 37 | 36 | ThreadPoolExecutor executor = new ThreadPoolExecutor( |
| 38 | | 0, // 0 so for unused thread pools threads will eventually die, freeing also the threadpool |
| 39 | | workers, // do not this number of threads |
| | 37 | 0, // keep core pool the same size as max |
| | 38 | workers, // do not exceed this number of threads |
| 40 | 39 | 300, // keepalive for thread |
| 41 | 40 | TimeUnit.SECONDS, |
| 42 | | workQueue, |
| | 41 | new HostLimitQueue(queueLimit, workers), |
| 43 | 42 | Utils.newThreadFactory(nameFormat, Thread.NORM_PRIORITY) |
| 44 | 43 | ); |
| 45 | | workQueue.setExecutor(executor); |
| 46 | 44 | return executor; |
| 47 | 45 | } |
| 48 | 46 | |
| … |
… |
public class HostLimitQueueTest {
|
| 63 | 61 | public void run() { |
| 64 | 62 | try { |
| 65 | 63 | Thread.sleep(1000); |
| 66 | | } catch (InterruptedException e) { |
| | 64 | System.out.println("downloaded: " + getUrl().toString()); |
| | 65 | } catch (InterruptedException | IOException e) { |
| 67 | 66 | Logging.trace(e); |
| 68 | 67 | } finally { |
| 69 | 68 | this.counter.incrementAndGet(); |
| … |
… |
public class HostLimitQueueTest {
|
| 98 | 97 | AtomicInteger counter = new AtomicInteger(0); |
| 99 | 98 | long start = System.currentTimeMillis(); |
| 100 | 99 | for (int i = 0; i < 10; i++) { |
| | 100 | System.out.println("exec: " + i); |
| 101 | 101 | tpe.execute(new Task(cache, new URL("http://localhost/"+i), counter)); |
| 102 | 102 | } |
| 103 | 103 | tpe.shutdown(); |