To use intrinsic locking in Java you do
Object o = new Object()
...
sychronized (o) {
...
}
So one monitor already requires one Object i.e. 8 bytes or 16 bytes for 64bit (or 12 bytes for compressed ops and 64bit).
Now assume you want to use lots of these monitors e.g. for an array which one can synchronize over certain areas and that has better concurrency (entry based) than Collections.synchronizedList
. Then what is the most efficient way to implement this? Could I somehow use 2 nested locks for 4 entries or 3 for 8 etc? Or could I make use of "one lock per thread" e.g. in a ConcurrentHashMap<array_index, lock>
?