SlideShare ist ein Scribd-Unternehmen logo
1 von 13
ThreadLocal    :
    ThreadLocal myNode = new ThreadLocal() {  
             protected QNode initialValue() {  
                 return new QNode();  
             }                                       ThreadLocal<T> 能够访问 Thread 的
         }; 
                                                     threadLocalMap
                                                     key 是 threadLocal 对象,
                                                     value 是具体使用的 <T> 值
public T get() { 
Thread t = Thread.currentThread(); 
ThreadLocalMap map = getMap(t); 
if (map != null) { 
     ThreadLocalMap.Entry e = map.getEntry(this); 
     if (e != null) 
     return (T)e.value; 
} 
return setInitialValue(); } 
myNode

                                              myNode(ThreadLocal  )   
                    CLHLock          myPred



   Thread1  
                            tail
                                              myPred (ThreadLocal  )


   Thread 2         tail (Node)  


                      CLHLock  :
   Thread N           public void lock() {      
                         QNode node = myNode.get();  
                              node.locked = true;  
                              QNode pred = tail.getAndSet(node);  
Thread :                      myPred.set(pred);  
CLHLock.lock()                while (pred.locked) {  
doSomeThing();                }  
CLHLock.unlock();     }
CLHLock:  对象                                                 ThreadLocal    
                                                 myNode
myPred :对象域
node :局部变量
                            CLHLock              myPred
                                                              ThreadLocal   
myNodeKey :
                         myPred,myNode,tail
ThreadLocal myNode   
                                                  tail
as  Key
                            node 


                                 QNode                       QNode
                              locked=true                 locked=false 
                                     myNodeKey



                               Thread1  


                           Thread1  :  lock()
                           QNode node = myNode.get();  
                            node.locked = true;  
ThreadLocal    
                           myNode




    CLHLock                myPred
                                                ThreadLocal  
 myPred,myNode,tail


                  tail
    node                              pred


         QNode                             QNode
      locked=true                       locked=fasle 
               myNodeKey
                                    myPredKey


       Thread1  

            Thread1  :  lock() 
             QNode pred = tail.getAndSet(node);  
                    myPred.set(pred);  
            while (pred.locked) {  
                    }
ThreadLocal    
                                                myNode




                         CLHLock                myPred
                                                                     ThreadLocal  
                      myPred,myNode,tail


                                       tail
    node 


         QNode                QNode                             QNode
      locked=true          locked=true                       locked=fasle 
         myNodeKey                  myNodeKey
                                                         myPredKey



        Thread2                Thread1  



Thread2  :  lock()
QNode node = myNode.get();                         Thread1  :   
  node.locked = true;                              dosomeThing(){
                                                   ……
                                                   }  
                                                        
ThreadLocal    
                                                           myNode




                                  CLHLock                  myPred
                                                                                ThreadLocal  
                               myPred,myNode,tail


                                tail
       node                                           pred


            QNode                          QNode                           QNode
         locked=true                    locked=true                     locked=fasle 
              myNodeKey                        myNodeKey
                              myPredKey
                                                                    myPredKey


             Thread2                      Thread1  

Thread2  :  lock()                                            Thread1  :   
 QNode pred = tail.getAndSet(node);                           dosomeThing(){
        myPred.set(pred);  
 while (pred.locked) {  
                                                              ……
      LockSupport.park(this); //block                         }  
  }                                                                
  
ThreadLocal    
                                                             myNode




                                  CLHLock                     myPred
                                                                                   ThreadLocal  
                               myPred,myNode,tail


                                tail
       node                            node                pred


            QNode                          QNode                              QNode
         locked=true                    locked=false                       locked=fasle 
                                               myNodeKey
              myNodeKey
                              myPredKey
                                                                       myPredKey


             Thread2                      Thread1  

Thread2  :  lock()                                                Thread1  :  unlock () 
 QNode pred = tail.getAndSet(node);                               QNode node = myNode.get();  
        myPred.set(pred);                                          node.locked = false;  
 while (pred.locked) {  
      LockSupport.park(this); //block                              myNode.set(myPred.get());  
  }                                                                  
                                                                       
ThreadLocal    
                                                      myNode




                                CLHLock               myPred
                                                                           ThreadLocal  
                             myPred,myNode,tail


                              tail




          QNode                         QNode                         QNode 
       locked=true                   locked=false                  locked=fasle 
                                          myNodeKey
           myNodeKey
                            myPredKey
                                                               myPredKey


          Thread2                       Thread1  




Thread2  :  Dosomthing(){
}
  
ThreadLocal    
                                                myNode




                         CLHLock                myPred
                                                                     ThreadLocal  
                      myPred,myNode,tail


                        tail
    node 


        QNode                     QNode                         QNode
     locked=false              locked=false                  locked=fasle 
                                    myNodeKey
         myNodeKey
                      myPredKey
                                                         myPredKey


        Thread2                   Thread1  


Thread2  : unlock();
 QNode node = myNode.get();  
 node.locked = false;  
 myNode.set(myPred.get()); 
ThreadLocal    
                                           myNode




                    CLHLock                myPred
                                                                ThreadLocal  
                 myPred,myNode,tail


                  tail
                                                       node 


   QNode                    QNode                          QNode
locked=false             locked=false                   locked=true 
                               myNodeKey
   myNodeKey
                myPredKey
                                                    myPredKey


  Thread2                   Thread1  


                            Thread1  :  lock()
                            QNode node = myNode.get();  
                              node.locked = true;  
ThreadLocal    
                                             myNode




                    CLHLock                     myPred
                                                             ThreadLocal  
                 myPred,myNode,tail

                                      tail

pred                                                     node 


   QNode                   QNode                            QNode
locked=false            locked=false                     locked=true 
                        myPredKey   myNodeKey
   myNodeKey
                myPredKey



  Thread2                  Thread1  


                              Thread1  :  lock() 
                               QNode pred = tail.getAndSet(node);  
                                      myPred.set(pred);  
                                while (pred.locked) {  
                                      }

                                 
ThreadLocal    
                                             myNode




                    CLHLock                     myPred
                                                             ThreadLocal  
                 myPred,myNode,tail

                                      tail
                                                         node 


   QNode                   QNode                            QNode
locked=false            locked=false                     locked=true 
                        myPredKey   myNodeKey
   myNodeKey
                myPredKey



  Thread2                  Thread1  


                              Thread1  : lock() 后再次调用  lock() 
                              QNode node = myNode.get();  
                                node.locked = true;  
                                 
ThreadLocal    
                                           myNode




                    CLHLock                   myPred
                                                              ThreadLocal  
                 myPred,myNode,tail

                                    tail
                                                       node          pred


   QNode                   QNode                           QNode
locked=false            locked=false                    locked=true 
                                  myNodeKey
   myNodeKey
                myPredKey
                                                  myPredKey

  Thread2                   Thread1  

                            Thread1  :  lock() 
                             QNode pred = tail.getAndSet(node);  
                                    myPred.set(pred);  
                              while (pred.locked) { // 永远 true  不可重入锁
                                    }

                             

Weitere ähnliche Inhalte

Empfohlen

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Empfohlen (20)

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 

Clh spinlock 实现演示

  • 1. ThreadLocal    :    ThreadLocal myNode = new ThreadLocal() {               protected QNode initialValue() {                   return new QNode();               }   ThreadLocal<T> 能够访问 Thread 的         };  threadLocalMap key 是 threadLocal 对象, value 是具体使用的 <T> 值 public T get() {  Thread t = Thread.currentThread();  ThreadLocalMap map = getMap(t);  if (map != null) {  ThreadLocalMap.Entry e = map.getEntry(this);  if (e != null)  return (T)e.value;  }  return setInitialValue(); } 
  • 2. myNode myNode(ThreadLocal  )    CLHLock   myPred Thread1   tail myPred (ThreadLocal  ) Thread 2  tail (Node)   CLHLock  : Thread N  public void lock() {          QNode node = myNode.get();           node.locked = true;           QNode pred = tail.getAndSet(node);   Thread :         myPred.set(pred);   CLHLock.lock()         while (pred.locked) {   doSomeThing();         }   CLHLock.unlock(); }
  • 3. CLHLock:  对象 ThreadLocal     myNode myPred :对象域 node :局部变量 CLHLock  myPred ThreadLocal    myNodeKey :  myPred,myNode,tail ThreadLocal myNode    tail as  Key node  QNode QNode locked=true  locked=false  myNodeKey Thread1   Thread1  :  lock() QNode node = myNode.get();    node.locked = true;  
  • 4. ThreadLocal     myNode CLHLock  myPred ThreadLocal    myPred,myNode,tail tail node  pred QNode QNode locked=true  locked=fasle  myNodeKey myPredKey Thread1   Thread1  :  lock()   QNode pred = tail.getAndSet(node);           myPred.set(pred);   while (pred.locked) {           }
  • 5. ThreadLocal     myNode CLHLock  myPred ThreadLocal    myPred,myNode,tail tail node  QNode QNode QNode locked=true  locked=true  locked=fasle  myNodeKey myNodeKey myPredKey Thread2   Thread1   Thread2  :  lock() QNode node = myNode.get();   Thread1  :      node.locked = true;   dosomeThing(){ …… }    
  • 6. ThreadLocal     myNode CLHLock  myPred ThreadLocal    myPred,myNode,tail tail node  pred QNode QNode QNode locked=true  locked=true  locked=fasle  myNodeKey myNodeKey myPredKey myPredKey Thread2   Thread1   Thread2  :  lock() Thread1  :     QNode pred = tail.getAndSet(node);   dosomeThing(){         myPred.set(pred);   while (pred.locked) {   ……       LockSupport.park(this); //block }     }            
  • 7. ThreadLocal     myNode CLHLock  myPred ThreadLocal    myPred,myNode,tail tail node  node  pred QNode QNode QNode locked=true  locked=false  locked=fasle  myNodeKey myNodeKey myPredKey myPredKey Thread2   Thread1   Thread2  :  lock() Thread1  :  unlock ()   QNode pred = tail.getAndSet(node);   QNode node = myNode.get();           myPred.set(pred);    node.locked = false;   while (pred.locked) {         LockSupport.park(this); //block  myNode.set(myPred.get());     }                
  • 8. ThreadLocal     myNode CLHLock  myPred ThreadLocal    myPred,myNode,tail tail QNode QNode QNode  locked=true  locked=false  locked=fasle  myNodeKey myNodeKey myPredKey myPredKey Thread2   Thread1   Thread2  :  Dosomthing(){ }   
  • 9. ThreadLocal     myNode CLHLock  myPred ThreadLocal    myPred,myNode,tail tail node  QNode QNode QNode locked=false  locked=false  locked=fasle  myNodeKey myNodeKey myPredKey myPredKey Thread2   Thread1   Thread2  : unlock();  QNode node = myNode.get();    node.locked = false;    myNode.set(myPred.get()); 
  • 10. ThreadLocal     myNode CLHLock  myPred ThreadLocal    myPred,myNode,tail tail node  QNode QNode QNode locked=false  locked=false  locked=true  myNodeKey myNodeKey myPredKey myPredKey Thread2   Thread1   Thread1  :  lock() QNode node = myNode.get();     node.locked = true;  
  • 11. ThreadLocal     myNode CLHLock  myPred ThreadLocal    myPred,myNode,tail tail pred node  QNode QNode QNode locked=false  locked=false  locked=true  myPredKey myNodeKey myNodeKey myPredKey Thread2   Thread1   Thread1  :  lock()   QNode pred = tail.getAndSet(node);           myPred.set(pred);   while (pred.locked) {           }    
  • 12. ThreadLocal     myNode CLHLock  myPred ThreadLocal    myPred,myNode,tail tail node  QNode QNode QNode locked=false  locked=false  locked=true  myPredKey myNodeKey myNodeKey myPredKey Thread2   Thread1   Thread1  : lock() 后再次调用  lock()  QNode node = myNode.get();     node.locked = true;      
  • 13. ThreadLocal     myNode CLHLock  myPred ThreadLocal    myPred,myNode,tail tail node  pred QNode QNode QNode locked=false  locked=false  locked=true  myNodeKey myNodeKey myPredKey myPredKey Thread2   Thread1   Thread1  :  lock()   QNode pred = tail.getAndSet(node);           myPred.set(pred);   while (pred.locked) { // 永远 true  不可重入锁         }