notes for abstractqueuesynchronizer

##Note for AbstractQueueSynchronizer

  • Synchronizer process two kind of operation:
    1. acquire
    2. release
  • operation of acquire as:
1
2
3
4
5
if(synchronization state does not allow acquire){
enqueue current thread if not already queued;
block current thread
}
dequeue current thread if it was queued;

  • operation of release as:

    1
    2
    3
    4
    update synchronization state;
    if(state may permit a blocked thread to acquire){
    unblock one or more queued threads;
    }
  • Support for these operation require the coordination of these basic components:

    1. Atomically managing synchronization state;
    2. Blocking and unblocking thread;
    3. Maintaing queue;
  • AbstractQueuedSynchronizer maintains synchronization state using only a single (32bit) int.

  • LockSupport.park operation issued the problem of Thread.suspend while multiple resume called before suspend through not counted.

Queue

  • There are two candicates to implemtent queue:

    • MCS(Mellor-Crummey and Scott).In fact,I don’t know what it is.
    • CLH,a low-level locks that is used only in spinlock.
  • The main additional modification needed to use CLH queues for blocking synchronizer is to provide an efficiency way for one node to locate its successor.

    • Once it need change,in spinlocks,the node will be notified on next spin by its successor.
    • But in blocking queue,the node need to explicitly wake up its successor by Lock.unpark.
  • To provide an atomically insertion node opertion.Because of the speciality of locate opertion,the node only need guarantee entail is atomic.

Usage

  • Concrete subclass need to implement tryAcquire and tryRelease through by inspecting and updating state.

  • Shared-mode,ReentrantReadWriteLock,will wake up multiple thread by cascading singals.