Ignore:
Timestamp:
2018-09-19T21:57:27+02:00 (6 years ago)
Author:
wiktorn
Message:

Use corePoolSize = maxPoolSize and unbounded queue

Remove hack with calling setCorePoolSize to spawn new threads when already
corePoolSize is running. For bigger sizes of the ThreadPool setCorePoolSize took
a long time and blocked EDT thread. Now we will have always `maximum concurren
downloads` threads running for TMS layers, but this is not a big issue, as the
thread pool is shared between imagery layers.

As long as number of threads will not reach the maximum concurrent downloads
the maximum concurrent downloads per host limit will not be enforced (first n
downloads).

See: #16734

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/data/cache/HostLimitQueueTest.java

    r13733 r14261  
    1414import org.junit.Rule;
    1515import org.junit.Test;
     16import org.openstreetmap.josm.data.imagery.TMSCachedTileLoader;
    1617import org.openstreetmap.josm.data.imagery.TileJobOptions;
    1718import org.openstreetmap.josm.testutils.JOSMTestRules;
    1819import org.openstreetmap.josm.tools.Logging;
    19 import org.openstreetmap.josm.tools.Utils;
    2020
    2121import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     
    3333    public JOSMTestRules test = new JOSMTestRules().preferences().timeout(20 * 1000);
    3434
    35     private static ThreadPoolExecutor getNewThreadPoolExecutor(String nameFormat, int workers, int queueLimit) {
    36         HostLimitQueue workQueue = new HostLimitQueue(queueLimit);
    37         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
    40                 300, // keepalive for thread
    41                 TimeUnit.SECONDS,
    42                 workQueue,
    43                 Utils.newThreadFactory(nameFormat, Thread.NORM_PRIORITY)
    44                 );
    45         workQueue.setExecutor(executor);
    46         return executor;
    47     }
    4835
    4936    /**
     
    9481    @Test
    9582    public void testSingleThreadPerHost() throws Exception {
    96         ThreadPoolExecutor tpe = getNewThreadPoolExecutor("test-%d", 3, 1);
     83        ThreadPoolExecutor tpe = TMSCachedTileLoader.getNewThreadPoolExecutor("test-%d", 3, 1);
    9784        ICacheAccess<String, CacheEntry> cache = JCSCacheManager.getCache("test", 3, 0, "");
    9885        AtomicInteger counter = new AtomicInteger(0);
     
    10794        assertEquals(10, counter.get());
    10895        // although there are 3 threads, we can make only 1 parallel call to localhost
    109         // so it should take ~10 seconds to finish
     96        // first three jobs will be not limited, as they spawn the thread
     97        // so it should take ~8 seconds to finish
    11098        // if it's shorter, it means that host limit does not work
    111         assertTrue("Expected duration between 9 and 11 seconds not met. Actual duration: " + (duration /1000),
    112                 duration < 11*1000 & duration > 9*1000);
     99        assertTrue("Expected duration between 8 and 11 seconds not met. Actual duration: " + (duration /1000),
     100                duration < 11*1000 & duration > 8*1000);
    113101    }
    114102
     
    119107    @Test
    120108    public void testMultipleThreadPerHost() throws Exception {
    121         ThreadPoolExecutor tpe = getNewThreadPoolExecutor("test-%d", 3, 2);
     109        ThreadPoolExecutor tpe = TMSCachedTileLoader.getNewThreadPoolExecutor("test-%d", 3, 2);
    122110        ICacheAccess<String, CacheEntry> cache = JCSCacheManager.getCache("test", 3, 0, "");
    123111        AtomicInteger counter = new AtomicInteger(0);
     
    144132    @Test
    145133    public void testTwoHosts() throws Exception {
    146         ThreadPoolExecutor tpe = getNewThreadPoolExecutor("test-%d", 3, 1);
     134        ThreadPoolExecutor tpe = TMSCachedTileLoader.getNewThreadPoolExecutor("test-%d", 3, 1);
    147135        ICacheAccess<String, CacheEntry> cache = JCSCacheManager.getCache("test", 3, 0, "");
    148136        AtomicInteger counter = new AtomicInteger(0);
Note: See TracChangeset for help on using the changeset viewer.