I am trying to implement some kind of map with the following characteristics.
Every item should be removed after the time-to-live has elapsed (unless time-to-live is 0 and then the item does not expire) get, put, remove, size should all have a complexity of o(1) in average
I have called a thread inside a constructor that goes in a while(true)
loop and check if the ttl has expired
the code of the thread
class ttlWatchdog extends Thread {
@SneakyThrows
@Override
public void run() {
long timeToSleep ;
System.out.println("Initiating Cleaner Thread..");
while (true) {
timeToSleep = checkExpiredKeys();//need to change it to a diffrent
//System.out.println("thread will sleep for " + timeToSleep + "msc" );
Thread.sleep(timeToSleep);
}
}
public long checkExpiredKeys() throws BadParameterException {
// Just remove if timeToLive has been set before...
long timeToWake = 0;
long currentTime = System.currentTimeMillis();
int counter =0 ;
Set<Map.Entry<String, ObjectTimeStampPair> >s = valuesMap.entrySet().stream().filter(a-> a.getValue().getTtl()!=0).collect(Collectors.toSet());
for (Map.Entry<String, ObjectTimeStampPair> e : s) {
long timeItShouldRemove = e.getValue().getTtl();
if ((currentTime ) >= timeItShouldRemove) {
remove(e.getKey());
}else {// we need to take the minimum for the while loop everything here is bigger than current time i need to find the minumum
if (counter == 0 ){ // if it is the first element take it
timeToWake = e.getValue().getTtl();
counter ++;
}
else if (timeToWake > timeItShouldRemove){
timeToWake = timeItShouldRemove;
}
}
}
long result = timeToWake !=0 ? timeToWake -currentTime : 0 ;
//System.out.print("the time to wake is " + timeToWake + " the current time is " + currentTime + " and the result is " + result);
return result;
//
}
my problem is with the while(true)
which is not very efficient especially when the map is empty or it is full of object with unlimited ttl.