Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions core/src/main/scala/kafka/utils/Pool.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import collection.JavaConverters._
class Pool[K,V](valueFactory: Option[K => V] = None) extends Iterable[(K, V)] {

private val pool: ConcurrentMap[K, V] = new ConcurrentHashMap[K, V]
private val createLock = new Object

def put(k: K, v: V): V = pool.put(k, v)

Expand Down Expand Up @@ -57,21 +56,10 @@ class Pool[K,V](valueFactory: Option[K => V] = None) extends Iterable[(K, V)] {
* @param createValue Factory function.
* @return The final value associated with the key.
*/
def getAndMaybePut(key: K, createValue: => V): V = {
val current = pool.get(key)
if (current == null) {
createLock synchronized {
val current = pool.get(key)
if (current == null) {
val value = createValue
pool.put(key, value)
value
}
else current
}
}
else current
}
def getAndMaybePut(key: K, createValue: => V): V =
pool.computeIfAbsent(key, new java.util.function.Function[K, V] {
override def apply(k: K): V = createValue
})

def contains(id: K): Boolean = pool.containsKey(id)

Expand Down