Advertisement
Guest User

Untitled

a guest
Mar 13th, 2014
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.53 KB | None | 0 0
  1.     private val cache = new java.util.concurrent.ConcurrentHashMap[T, java.lang.Long]()
  2.     var currentThreshold = 1L
  3.    
  4.     {
  5.         val sizeMonitor = new Thread(new Runnable {
  6.             override def run() {
  7.                 while (true) {
  8.                     Thread.sleep(1000)
  9.                     if (MemoryPressureMonitor.haveMemoryPressure(10)) {
  10.                         val initialSize = cache.size()
  11.                         val entryIterator = cache.entrySet().iterator()
  12.                         var foundMinimum = Long.MaxValue
  13.                         var removedEnough = false
  14.                         while (!removedEnough) {
  15.                             while (entryIterator.hasNext) {
  16.                                 val nextValue = entryIterator.next()
  17.                                 if (nextValue.getValue <= currentThreshold)
  18.                                     entryIterator.remove()
  19.                                 else
  20.                                 if (nextValue.getValue < foundMinimum)
  21.                                     foundMinimum = nextValue.getValue
  22.                             }
  23.                             removedEnough = cache.size().toDouble / initialSize <= 0.9
  24.                             if (!removedEnough)
  25.                                 currentThreshold = foundMinimum
  26.                         }
  27.                     }
  28.                 }
  29.             }
  30.         })
  31.         sizeMonitor.setName("ObjectCounter-size-monitor")
  32.         sizeMonitor.setDaemon(true)
  33.         sizeMonitor.start()
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement