Scala/Java 中的等价锁?
Equivalence lock in Scala/Java?
在 Scala/Java 例如
中是否有锁定对象相等性而不是引用相等性
def run[A](id: A) = id.synchronized {
println(s"Processing $id")
Thread.sleep(5000)
println(s"Done processing $id")
}
Seq(1, 1, 2, 3, 1).par.foreach(run)
我希望它打印如下内容:
Processing 3
Processing 1
Processing 2
// 5 seconds later
Done processing 1
Done processing 2
Done processing 3
Processing 1
// 5 seconds later
Done processing 1
Processing 1
// 5 seconds later
Done processing 1
我能想到的最好的是这样的:
import scala.collection.mutable
class EquivalenceLock[A] {
private[this] val keys = mutable.Map.empty[A, AnyRef]
def apply[B](key: A)(f: => B): B = {
val lock = keys.synchronized(keys.getOrElseUpdate(key, new Object()))
lock.synchronized(f)
}
}
然后将其用作:
def run[A](id: A)(implicit lock: EquivalenceLock[A]) = lock(id) {
println(s"Processing $id")
Thread.sleep(5000)
println(s"Done processing $id")
}
编辑:使用评论中提到的锁条带化,这是一个简单的实现:
/**
* An util that provides synchronization using value equality rather than referential equality
* It is guaranteed that if two objects are value-equal, their corresponding blocks are invoked mutually exclusively.
* But the converse may not be true i.e. if two objects are not value-equal, they may be invoked exclusively too
*
* @param n There is a 1/(2^n) probability that two invocations that could be invoked concurrently is not invoked concurrently
*
* Example usage:
* private[this] val lock = new EquivalenceLock()
* def run(person: Person) = lock(person) { .... }
*/
class EquivalenceLock(n: Int) {
val size = 1<<n
private[this] val locks = IndexedSeq.fill(size)(new Object())
def apply[A](lock: Any) = locks(lock.hashCode().abs & (size - 1)).synchronized[A] _ // synchronize on the (lock.hashCode % locks.length)th object
}
object EquivalenceLock {
val defaultInstance = new EquivalenceLock(10)
}
如果您不想在这里重新发明轮子,Guava's Striped 最适合。
在 Scala/Java 例如
中是否有锁定对象相等性而不是引用相等性def run[A](id: A) = id.synchronized {
println(s"Processing $id")
Thread.sleep(5000)
println(s"Done processing $id")
}
Seq(1, 1, 2, 3, 1).par.foreach(run)
我希望它打印如下内容:
Processing 3
Processing 1
Processing 2
// 5 seconds later
Done processing 1
Done processing 2
Done processing 3
Processing 1
// 5 seconds later
Done processing 1
Processing 1
// 5 seconds later
Done processing 1
我能想到的最好的是这样的:
import scala.collection.mutable
class EquivalenceLock[A] {
private[this] val keys = mutable.Map.empty[A, AnyRef]
def apply[B](key: A)(f: => B): B = {
val lock = keys.synchronized(keys.getOrElseUpdate(key, new Object()))
lock.synchronized(f)
}
}
然后将其用作:
def run[A](id: A)(implicit lock: EquivalenceLock[A]) = lock(id) {
println(s"Processing $id")
Thread.sleep(5000)
println(s"Done processing $id")
}
编辑:使用评论中提到的锁条带化,这是一个简单的实现:
/**
* An util that provides synchronization using value equality rather than referential equality
* It is guaranteed that if two objects are value-equal, their corresponding blocks are invoked mutually exclusively.
* But the converse may not be true i.e. if two objects are not value-equal, they may be invoked exclusively too
*
* @param n There is a 1/(2^n) probability that two invocations that could be invoked concurrently is not invoked concurrently
*
* Example usage:
* private[this] val lock = new EquivalenceLock()
* def run(person: Person) = lock(person) { .... }
*/
class EquivalenceLock(n: Int) {
val size = 1<<n
private[this] val locks = IndexedSeq.fill(size)(new Object())
def apply[A](lock: Any) = locks(lock.hashCode().abs & (size - 1)).synchronized[A] _ // synchronize on the (lock.hashCode % locks.length)th object
}
object EquivalenceLock {
val defaultInstance = new EquivalenceLock(10)
}
如果您不想在这里重新发明轮子,Guava's Striped 最适合。