首页 > 解决方案 > 调试自定义多线程缓存

问题描述

我有一个包含 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);
        }
    }
}

是否有可能一个线程超过了最大堆限制并且抛出了错误,但是由于我没有发现错误,所以我没有观察到它?

但我不确定如果一个线程抛出内存不足错误,那么整个应用程序会停止还是只有一个线程会受到影响?任何意见?

标签: javamultithreadingcaching

解决方案


推荐阅读