当前位置:网站首页>AQS learning notes

AQS learning notes

2022-06-22 08:26:00 ambition_ forever

 1、 The first day

ReentrantLock
   AQS
    Program  =  thought  +  Code 
    thought  =  Solutions to problems 
    Banks do business 
   1、 It depends on how many people handle it , Just go to the counter , Line up more often 
   2、 There is waiting in the queue 
   3、 If the president's brother-in-law comes to do business , There's definitely no need to queue up , Go straight to business .
         Unfair 
   4、 It's too late , Rearrange 
       if (failed)
         cancelAcquire(node);
   5、 A simple operation 
      A&&B
      A||B
   6、 Six operations 
       Grab the lock 
        1、 Look at the lock flag  state = 0  By default, no thread holds the lock  state = 1  A thread holds a lock 
        2、state = 0  No thread holds the lock , Locking requires the thread to modify the lock flag bit , It needs a mechanism that only one thread can modify the success lock flag at the same time =CAS
        3、state >= 1 The wired stack has a lock , Is the current thread that grabs the lock the thread that owns the lock .
             yes , Reentrant  state+1
             No , Lock snatch failed 
        4、 Optimize : See if there is anyone in the waiting area , If someone , The lock must be occupied .
             If it is judged that someone is waiting  hasQueuedPredecessors
         Fair lock 、 There are differences in unfair locks .
         The only difference between fair locks and unfair locks is that there is a critical area ,
             The thread that previously held the lock has executed the external task , Lock released , At this time, a thread happens to come ( Not fair lock ).
         It has the meaning of existence in reality .

          Fair lock 
         if (c == 0) {
             if (!hasQueuedPredecessors() &&
                 compareAndSetState(0, acquires)) {
                 setExclusiveOwnerThread(current);
                 return true;

         }

         Not fair lock 
        if (c == 0) {
          if (compareAndSetState(0, acquires)) {
             setExclusiveOwnerThread(current);
             return true;
           }
        }

        Node node 
        CLH Algorithm , One way queue 
        AQS  The bidirectional queue 


       Release the lock 
        1、 Reentry problem 
        2、 The lock status bit returns to 0
        3、 Wake up the waiting thread in the queue 


       The team 
        If you don't grab the lock , And not reentrant , Then you need to join the team .
       !tryAcquire(arg)  Locking failed 
       acquireQueued(addWaiter(Node.EXCLUSIVE), arg) Need to be queued , Turn the present into Node, Then set the alarm clock to block .
       public final void acquire(int arg) {
            if (!tryAcquire(arg) &&
                acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
                selfInterrupt();
       }
        set alarm  waitStatus
         AQS queue 
            The first node in the queue , The node corresponding to the thread that holds the lock .
            Queue entry , There are no nodes in the queue ,
                   There is no such thing as ,AQS The queue generates two nodes 
                   Possessive ,AQS Tail insertion .
            Outgoing queue 

       Out of the team 
         The task of leaving the queue is left to the awakened thread .

       Blocking 
        After entering the queue , What do you do 
                      The second node in the queue grabs the lock again , Because the first node is the node that holds the lock . Try to see before blocking .
                      Set the alarm clock 
                      Blocking 

                      Spin twice 
                          Of a precursor node waitStatus It is amended as follows -1, then park 了 .

       Wake up the 

2、 the second day

        To be continued ....

原网站

版权声明
本文为[ambition_ forever]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220528291607.html