Clh spinlock 实现演示
Upcoming SlideShare
Loading in...5
×
 

Clh spinlock 实现演示

on

  • 335 views

 

Statistics

Views

Total Views
335
Views on SlideShare
314
Embed Views
21

Actions

Likes
0
Downloads
3
Comments
1

2 Embeds 21

http://www.topdigger.tk 13
http://topdigger.sinaapp.com 8

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • 比在slideboom清晰多了
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Clh spinlock 实现演示 Clh spinlock 实现演示 Presentation Transcript

    • 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     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;  
    • 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 QNodelocked=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 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) {           }    
    • 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;      
    • 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  不可重入锁         }