Multithreading study five faster than the read-write lock lock -StampedLock

StampedLock supports three modes: write locks, read pessimistic, optimistic reading

StampedLock ReadWriteLock performance than good reason to support the optimistic reading.

readwritelock support multi-threaded read, but when to write, read all are blocked. The stampedlock supports write, they also read, because reading is not optimistic lock.

When a thread is blocked in stampedlock readlock or writelock, this time calling thread interrupt can cause obstruction cup soar. At this point, you should call readlockInterrupt or writelockinterrupt

Common wording

final StampedLock sl = 
  new StampedLock();

// optimistic read
Long stamp = 
  sl.tryOptimisticRead ();
// read method of local variables
......
// check stamp
(! Sl.validate (stamp)) IF {
  // update pessimistic read locks
  stamp sl.readLock = ();
  the try {
    // read method of local variables
    .....
  } the finally {
    // release pessimistic read locks
    sl.unlockRead (Stamp);
  }
}
// use local variables performs traffic operations
.. ....
 

发布了23 篇原创文章 · 获赞 19 · 访问量 1417

Guess you like

Origin blog.csdn.net/u012335601/article/details/89600702