Search code examples
javamultithreadingscheduledexecutorservice

How to make ScheduleTask perform a task once at a certain time?


recently working with ScheduleTask and I have such a question. I want that my ScheduleTask removes elements from my map every 5 seconds when map size more than MAX_SIZE field. I'm trying do this way:

public class RemoverThread extends TimerTask {

    private AbstractCustomCache customCache;
    private static final int MAX_SIZE = 2;

    public RemoverThread(AbstractCustomCache customCache) {
        this.customCache = customCache;
    }

    @Override
    public void run() {
        if (customCache.getCacheEntry().size() > MAX_SIZE) {
            List<String> gg = new ArrayList<>(customCache.getCacheEntry().keySet());
            for (String s : gg) {
                System.out.println("Element was remove: " + customCache.getCacheEntry().remove(s));
                System.out.println("Time: " + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(Calendar.getInstance().getTime()));
                if (customCache.getCacheEntry().size() <= MAX_SIZE) {
                    break;
                }
            }
        }
    }
}

My Main:

public class Main {
    public static void main(String[] args) {
        AbstractCustomCacheStatistics gh = new MapCacheStatistics();
        AbstractCustomCache gg = new MapCache(gh);


        for (int i = 0; i < 5; i++){
            gg.put("ab" + i);
        }
        RemoverThread removerThread = new RemoverThread(gg);
        Executors.newScheduledThreadPool(2).scheduleAtFixedRate(removerThread, 5, 5, TimeUnit.SECONDS);

        for (int i = 0; i < 3; i++){
            gg.put("abc" + i);
        }
    }
}

AbstractCustomCache:

public abstract class AbstractCustomCache implements CustomCache{

    private Map<String, CacheEntry> cacheEntry = new LinkedHashMap<>();

    public Map<String, CacheEntry> getCacheEntry() {
        return Collections.synchronizedMap(cacheEntry);
    }
}

I've got this in my output:

Element was remove: CacheEntry{field='ab0'}
Time: 2019/01/31 11:39:11
Element was remove: CacheEntry{field='ab1'}
Time: 2019/01/31 11:39:11
Element was remove: CacheEntry{field='ab2'}
Time: 2019/01/31 11:39:11
Element was remove: CacheEntry{field='ab3'}
Time: 2019/01/31 11:39:11
Element was remove: CacheEntry{field='ab4'}
Time: 2019/01/31 11:39:11
Element was remove: CacheEntry{field='abc0'}
Time: 2019/01/31 11:39:11

What am I doing wrong? How it can be improve? I want to delete from the map happened every 5 seconds. For example, I added ab0, ab1, ab2, ab3, ab4. The stream should remove ab0, ab1, ab2. Since the elements in the map are larger than MAX_SIZE. Then I add abc0, abc1, abc2. And 5 seconds after the removal of the first elements, ab3, ab4, abc0 should be removed. But, as you can see, all items are removed at the same time.


Solution

  • What am I doing wrong?

    1. with Executors.newScheduledThreadPool(2).scheduleAtFixedRate(removerThread, 5, 5, TimeUnit.SECONDS);, the first execute time will be delayed by 5 seconds, at that time the cache contains:ab0 ab1 ab2 ab3 ab4 abc0 abc1 ab2, the first 5 will be removed.

    2. You need a thread safe version AbstractCustomCache customCache since it is modified by multiple threads.