ThreadLocal    :    ThreadLocal myNode = new ThreadLocal() {               protected QNode initialValue() {               ...
myNode                                              myNode(ThreadLocal  )                       CLHLock          myPred   ...
CLHLock:  对象                                                 ThreadLocal                                                  ...
ThreadLocal                               myNode    CLHLock                myPred                                         ...
ThreadLocal                                                    myNode                         CLHLock                myPre...
ThreadLocal                                                               myNode                                  CLHLock ...
ThreadLocal                                                                 myNode                                  CLHLoc...
ThreadLocal                                                          myNode                                CLHLock        ...
ThreadLocal                                                    myNode                         CLHLock                myPre...
ThreadLocal                                               myNode                    CLHLock                myPred         ...
ThreadLocal                                                 myNode                    CLHLock                     myPred  ...
ThreadLocal                                                 myNode                    CLHLock                     myPred  ...
ThreadLocal                                               myNode                    CLHLock                   myPred      ...
Upcoming SlideShare
Loading in …5
×

Clh spinlock 实现演示

473 views

Published on

Published in: Technology, Business
1 Comment
0 Likes
Statistics
Notes
  • 比在slideboom清晰多了
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

No Downloads
Views
Total views
473
On SlideShare
0
From Embeds
0
Number of Embeds
27
Actions
Shares
0
Downloads
6
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Clh spinlock 实现演示

  1. 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. 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. 3. CLHLock:  对象 ThreadLocal     myNodemyPred :对象域node :局部变量 CLHLock  myPred ThreadLocal   myNodeKey :  myPred,myNode,tailThreadLocal myNode    tailas  Key node  QNode QNode locked=true  locked=false  myNodeKey Thread1   Thread1  :  lock() QNode node = myNode.get();    node.locked = true;  
  4. 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. 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. 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. 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. 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. 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. 10. ThreadLocal     myNode CLHLock  myPred ThreadLocal    myPred,myNode,tail tail node  QNode QNode QNodelocked=false  locked=false  locked=true  myNodeKey myNodeKey myPredKey myPredKey Thread2   Thread1   Thread1  :  lock() QNode node = myNode.get();     node.locked = true;  
  11. 11. ThreadLocal     myNode CLHLock  myPred ThreadLocal    myPred,myNode,tail tailpred node  QNode QNode QNodelocked=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. 12. ThreadLocal     myNode CLHLock  myPred ThreadLocal    myPred,myNode,tail tail node  QNode QNode QNodelocked=false  locked=false  locked=true  myPredKey myNodeKey myNodeKey myPredKey Thread2   Thread1   Thread1  : lock() 后再次调用  lock()  QNode node = myNode.get();     node.locked = true;      
  13. 13. ThreadLocal     myNode CLHLock  myPred ThreadLocal    myPred,myNode,tail tail node  pred QNode QNode QNodelocked=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  不可重入锁         }  

×