Skip to content

Instantly share code, notes, and snippets.

@sasssass
Created July 5, 2020 11:06
Show Gist options
  • Save sasssass/2e73ba83a7b2fc61df969fd51fa58685 to your computer and use it in GitHub Desktop.
Save sasssass/2e73ba83a7b2fc61df969fd51fa58685 to your computer and use it in GitHub Desktop.
abstract class ObjectPool<T>{
var expirationTime : Long = 5000;
var locked = HashMap<T,Long>()
var unLocked = HashMap<T,Long>()
abstract fun create() : T
abstract fun validate(obj : T) : Boolean
abstract fun expire (obj: T)
@Synchronized fun checkOut() : T{
val timeNow = System.currentTimeMillis()
if(unLocked.size > 0){
val e = unLocked.keys.toList()
for(item in e){
if(timeNow - unLocked[item]!! > expirationTime){
unLocked.remove(item)
expire(item)
}else if(validate(item)){
unLocked.remove(item)
locked.put(item,timeNow)
return item
}else{
unLocked.remove(item)
expire(item)
}
}
}
val ret = create()
locked.put(ret,timeNow)
return ret
}
@Synchronized fun checkIn(obj : T){
locked.remove(obj)
unLocked.put(obj,System.currentTimeMillis())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment