I am looking for a type of lock where a thread containing a lock can pass it to another allocated thread.
This is why I want:
- I have a class similar to
ConcurrentHashMap- a specialized set that is divided into several segments - Most modifications require only one segment to be locked. Some require locking two segments (in particular, changing a key so that it moves from one segment to another.)
- Most readings do not require locking - usually a volatile reading is sufficient, but sometimes you need to block all segments at once if the change counter is not checked.
- Search is performed in multiple threads (via
ThreadPoolExecutor) - It is important that the search function has a consistent view of all segments (for example, it should not skip recording when it moves from one segment to another.)
- The search task (for an individual segment) can be interrupted at any time.
Now I am considering a situation where the search method was called in the main thread and discovers that it needs to block all segments. All segment locks must be held simultaneously by the main thread (to ensure no interference), but this is not the main thread that will perform updates - this is one of the workflows. Therefore, I try to make the main thread pass locks when it knows that it has a consistent snapshot.
, .
( ReentrantLock) - , .
Semaphore . : - , ( , , .) , , .
, ( .)
, Semaphore AtomicBoolean AtomicReference, Google . , ?