[英]Is there a generic solution in Java for mutual exclusion between threads based on a generated key?
Lets say I have a FooClass
, which has an id
with type long: 可以说我有一个
FooClass
,其id
为long类型:
public class FooClass {
long id;
}
There is a method in whatever class with similar signature: 任何类中都有一个具有相似签名的方法:
public void shouldBeSynchronizedForAFooClassId(long fooClassId) {
//does something
}
What could I do: 我能做什么:
FooClass
with id
1 and FooClass
with id
2 can run parallel without problem. FooClass
与id
1和FooClass
与id
2可以毫无问题平行地延伸。 Map
with id as key and a monitored Object
as value. Object
为值的Map
。 This case I have to suffer with registering the number of threads using the same monitored Object to be able to cleanup the monitored Objects as soon as they are not used by any thread anymore. I thought, there must be a readymade solution like: 我认为,必须有一个现成的解决方案,例如:
Is there something like this out of the box in some library? 在某些库中是否开箱即用?
您可以使用所需的工具,但必须自己组装解决方案,例如ReentrantLocks 的映射
There isn't, as far as I know, any ready-made solution to the problem you're facing in the JDK. 据我所知,没有任何现成的解决方案可以解决您在JDK中面临的问题。
But Guava has Striped , which does almost what you want. 但是番石榴具有Striped ,几乎可以满足您的需求。 It doesn't associate a lock per ID, but rather a lock per "stripe" of IDs.
它不关联每个ID的锁,而是关联ID的“条带”的锁。 That should be sufficient to avoid contention.
这应该足以避免争执。
Instead of long id
, you could have ID id;
您可以使用
ID id;
代替long id
ID id;
where ID
is a class that you define. ID
是您定义的类。 Then you can simply lock the id
object. 然后,您可以简单地锁定
id
对象。
Your ID class can have a numeric value, or whatever other attributes you find convenient. 您的ID类可以具有数值,也可以具有其他方便的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.