I have a large array to be accessed by multiple thread. Single lock is not efficient enough.Is there a range lock class in ja开发者_如何转开发va or scala?
Not in the standard libraries. ConcurrentHashMap does this though, internally representing the hash table as "segments" (16 of them by default) where each one is protected with a separate lock. Also, this thread asks the same question except about an ArrayList instead of an array. Although fruitless, it brings up alternatives if you are able to compromise on usage.
Update: Perhaps AtomicReferenceArray and friends would provide the efficiency you're looking for while at the same time "provid[ing] volatile access semantics to the elements of the array" (JCIP 15.3).
Generaly speaking, except if you have a really specific need in term of concurrency, you will find very optimized and convenient objects in the java.util.concurrent package of jdk.
I can suggest Brian Goetz's "Java Concurrency in Practice", a very good book explaining a lot of things about threading in java and java.util.concurrent package also.
Wrap the array inside a thread safe object that controls access. You could either manage the ranges yourself, or split the array up into ranges, each with their own lock, and reference it that way.
精彩评论