Java ScheduledExecutorService scheduleWithFixedDelay在一段时间后停止刷新某些线程

时间:2019-05-31 10:10:01

标签: java

我有一个由32个缓存对象组成的数组,我每20秒就会定期刷新每个缓存。下面是附加的代码段。

我观察到在一些刷新周期后很少有缓存停止刷新,并且日志中没有打印异常。请注意,应用程序没有崩溃,但是某些缓存没有刷新。

public class CacheManager
{

    private static class CacheRefresher extends Thread
    {
        Cache cache; 

        public CacheRefresher(Cache cache)
        {
            this(cache);
        }

        @Override
        public final void run()
        {

                try {
                    LOG.info("cache is getting refreshed for " + cache.type);
                    cache.refreshCache();
                }
                catch (Exception e) {
                    String subject = "Cache refresh failed in BMW";
                    LOG.log(Level.WARN, subject + ". Exception thrown:", e);

                }
        }       
    }

    public void refreshCaches(List<cache> caches)
    {
        ThreadFactory ourThreadFactory =
                new NamedThreadFactory("CacheHandler", true);

        ScheduledExecutorService scheduleService =
                Executors.newScheduledThreadPool(32, ourThreadFactory);
        initialDelay = 60;
        for (Cache cache : caches) {  
            service.scheduleWithFixedDelay(new CacheRefresher(cache), initialDelay, 20, TimeUnit.SECONDS);
            initialDelay += 2;
            cacheContainers.add(cache);
        }
    }
}

进行线程转储后,我发现停止刷新的线程处于TIMED_WAITING状态。

"CacheHandler-19" daemon prio=5 Id=75 TIMED_WAITING on java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject@6b5fe7dc
        at java.base@11.0.2/jdk.internal.misc.Unsafe.park(Native Method)
        -  waiting on java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject@6b5fe7dc
        at java.base@11.0.2/java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:234)
        at java.base@11.0.2/java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2123)
        at java.base@11.0.2/java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1182)
        at java.base@11.0.2/java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:899)
        at java.base@11.0.2/java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1054)
        at java.base@11.0.2/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1114)
        at java.base@11.0.2/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)

0 个答案:

没有答案