Your SlideShare is downloading. ×
0
Clh spinlock 实现演示
Clh spinlock 实现演示
Clh spinlock 实现演示
Clh spinlock 实现演示
Clh spinlock 实现演示
Clh spinlock 实现演示
Clh spinlock 实现演示
Clh spinlock 实现演示
Clh spinlock 实现演示
Clh spinlock 实现演示
Clh spinlock 实现演示
Clh spinlock 实现演示
Clh spinlock 实现演示
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Clh spinlock 实现演示

271

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
271
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
1
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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

×