How can I run a background thread that cleans up some elements in list regularly?
<p>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.</p>

<pre><code>class Cache {
    int timeLimit = 10; //how long each entry needs to be kept after accessed(marked)
    int maxEntries = 10; //maximum number of Entries
    HashSet&lt;String&gt; set = new HashSet&lt;String&gt;();   
    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);
    }
    ....
}
</code></pre>

<p>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 <code>marked &amp;&amp; (last access time - now)&gt;timeLimit</code> ? </p>

<p><strong>edit</strong></p>

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