Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2027155
  • 博文数量: 413
  • 博客积分: 10926
  • 博客等级: 上将
  • 技术积分: 3862
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-09 18:14
文章分类

全部博文(413)

文章存档

2015年(5)

2014年(1)

2013年(5)

2012年(6)

2011年(138)

2010年(85)

2009年(42)

2008年(46)

2007年(26)

2006年(59)

分类: Java

2011-10-31 16:01:51

  1. Docs
  2. Packages
    java.util.concurrent.locks
    http://developer.android.com/reference/java/util/concurrent/locks/package-summary.html
  3. Class
    • Lock (ReentrantLock)

      (Docs:
      b) http://developer.android.com/reference/java/util/concurrent/locks/Lock.html
      a) http://developer.android.com/reference/java/util/concurrent/locks/ReentrantLock.html
      )

      A lock is a tool for controlling access to a shared resource by multiple threads. Commonly, a lock provides exclusive access to a shared resource: only one thread at a time can acquire the lock and all access to the shared resource requires that the lock be acquired first.
      Sample code:
      Lock l = new ReentrantLock();
      l.lock();
      try
      {
               // access the resource protected by this lock
      }
      finally
      {
               l.unlock();
      }
    • ReadWriteLock (ReentrantReadWriteLock, ReentrantReadWriteLock.ReadLock, ReentrantReadWriteLock.WriteLock)

      (Docs:
      a) http://developer.android.com/reference/java/util/concurrent/locks/ReadWriteLock.html
      b) http://developer.android.com/reference/java/util/concurrent/locks/ReentrantReadWriteLock.html

      )

      A ReadWriteLock maintains a pair of associated locks, one for read-only operations and one for writing. The read lock may be held simultaneously by multiple reader threads, so long as there are no writers. The write lock is exclusive.  That means only a single thread at a time (a writer thread) can modify the shared data, in many cases any number of threads can concurrently read the data (hence reader threads).

      Reentrancy also allows downgrading from the write lock to a read lock, by acquiring the write lock, then the read lock and then releasing the write lock. However, upgrading from a read lock to the write lock is not possible.

      Please refer http://developer.android.com/reference/java/util/concurrent/locks/ReentrantReadWriteLock.html to get sample.
    • Condition

      (Docs & sample: http://developer.android.com/reference/java/util/concurrent/locks/Condition.html)

    • xxx
  4. Implement
    • What's Read/Write lock
      (From: )
      1. 简介
      读写锁读写锁是一种特殊的自旋锁,它把对共享资源的访问者划分成读者和写者,读者只对共享资源进行读访问,写者则需要对共享资源进行写操作。写者是排他性的,一个读写锁同时只能有一个写者或多个读者(与CPU数相关),但不能同时既有读者又有写者。
      在读写锁保持期间也是抢占失效的。如果读写锁当前没有读者,也没有写者,那么写者可以立刻获得读写锁,否则它必须自旋在那里,直到没有任何写者或读者。如果读写锁没有写者,那么读者可以立即获得该读写锁,否则读者必须自旋在那里,直到写者释放该读写锁。  

      2. 特性:   
      一次只有一个线程可以占有写模式的读写锁, 但是可以有多个线程同时占有读模式的读写锁. 当读写锁是写加锁状态时, 在这个锁被解锁之前, 所有试图对这个锁加锁(读或写模式)的线程都会被阻塞.当读写锁在读加锁状态时, 所有试图以读模式对它进行加锁的线程都可以得到访问权, 但是如果线程希望以写模式对此锁进行加锁, 它必须阻塞直到所有的线程释放锁.   

      通常, 当读写锁处于读模式锁住状态时, 如果有另外线程试图以写模式加锁, 读写锁通常会阻塞随后的读模式锁请求, 这样可以避免读模式锁长期占用, 而等待的写模式锁请求长期阻塞.   

      3. 适用性:   
      读写锁适合于对数据结构的读次数比写次数多得多的情况. 因为, 读模式锁定时可以共享, 以写模式锁住时意味着独占, 所以读写锁又叫共享-独占锁.
    • xxx
  5. xxx
阅读(1672) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~