All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pl.touk.nussknacker.ui.db.timeseries.questdb.ThreadAwareObjectPool.scala Maven / Gradle / Ivy

There is a newer version: 1.17.0
Show newest version
package pl.touk.nussknacker.ui.db.timeseries.questdb

import scala.collection.mutable

private[questdb] class ThreadAwareObjectPool[T <: AutoCloseable](objectFactory: () => T) {
  private val pool = new mutable.WeakHashMap[Thread, T]()

  def get(): T = {
    val thread = Thread.currentThread()
    pool.getOrElse(
      thread, {
        val t = objectFactory()
        pool.put(thread, t)
        t
      }
    )
  }

  def clear(): Unit = {
    val values = pool.values.toList
    pool.clear()
    values.foreach(_.close())
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy