如何运行后台线程来定期清理列表中的某些元素? [英] How can I run a background thread that cleans up some elements in list regularly?

查看:77
本文介绍了如何运行后台线程来定期清理列表中的某些元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在实施缓存。我已完成基本实现,如下所示。我想要做的是运行一个线程,删除满足特定条件的条目。

I am currently implementing cache. I have completed basic implementation, like below. What I want to do is to run a thread that will remove entry that satisfy certain conditions.

class Cache {
    int timeLimit = 10; //how long each entry needs to be kept after accessed(marked)
    int maxEntries = 10; //maximum number of Entries
    HashSet<String> set = new HashSet<String>();   
    public void add(Entry t){
        ....
    }

    public Entry access(String key){
        //mark Entry that it has been used
        //Since it has been marked, background thread should remove this entry after timeLimit seconds.
        return set.get(key);
    }
    ....
}

我的问题是,我应该如何实现后台线程,以便线程绕过集合中的条目并删除已经标记的&& (上次访问时间 - 现在)> timeLimit

My question is, how should I implement background thread so that the thread will go around the entries in set and remove the ones that has been marked && (last access time - now)>timeLimit ?

编辑

以上只是代码的简化版本,我没有写同步语句。

Above is just simplified version of codes, that I did not write synchronized statements.

推荐答案

你为什么要重新发明轮子? EhCache (以及任何体面的缓存实施)都将为您完成此任务。还有更轻量级的 MapMaker Guava 可以自动删除旧条目。

Why are you reinventing the wheel? EhCache (and any decent cache implementation) will do this for you. Also much more lightweight MapMaker Cache from Guava can automatically remove old entries.

如果真的想要实施这就是你自己,它并不是那么简单。

If you really want to implement this yourself, it is not really that simple.


  1. 记住同步。您应该使用 ConcurrentHashMap synchronized 关键字来存储条目。这可能非常棘手。

  1. Remember about synchronization. You should use ConcurrentHashMap or synchronized keyword to store entries. This might be really tricky.

您必须以某种方式存储每个条目的最后访问时间。每次访问条目时,都必须更新该时间戳。

You must store last access time somehow of each entry somehow. Every time you access an entry, you must update that timestamp.

考虑驱逐政策。如果你的缓存中有超过 maxEntries ,那么首先删除哪些?

Think about eviction policy. If there are more than maxEntries in your cache, which ones to remove first?

你真的吗?需要一个后台线程吗?

Do you really need a background thread?

这是令人惊讶的,但是EhCache(企业就绪且经过验证)不使用后台线程来使旧条目无效)。相反,它等待地图填满并懒惰地删除条目。这似乎是一个很好的权衡因为线程很昂贵。

This is surprising, but EhCache (enterprise ready and proven) does not use background thread to invalidate old entries). Instead it waits until the map is full and removes entries lazily. This looks like a good trade-off as threads are expensive.

如果你有一个后台线程,那么每个缓存还是一个全局?您是在创建新缓存时启动新线程还是拥有所有缓存的全局列表?这比你想象的要难......

If you have a background thread, should there be one per cache or one global? Do you start a new thread while creating a new cache or have a global list of all caches? This is harder than you think...

一旦你回答了所有这些问题,实施起来相当简单:go每隔一秒左右通过所有条目,如果满足您已编写的条件,请删除该条目。

Once you answer all these questions, the implementation is fairly simple: go through all the entries every second or so and if the condition you've already written is met, remove the entry.

这篇关于如何运行后台线程来定期清理列表中的某些元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆