2005-10-31
Read/Write Lock
关键字: reader-write-lockpublic class SomeContainer { private Set elements; private ReadWriteLock globalLock; private Lock readLock; private Lock writeLock; SomeContainer() { elements = new HashSet(); globalLock = new ReentrantReadWriteLock(); readLock = globalLock.readLock(); writeLock = globalLock.writeLock(); } public void addElement(Element elem) { writeLock.lock(); try { elements.add(elem); } finally { writeLock.unlock(); } } public void processElements(ElementProcessor processor) { readLock.lock(); try { Iterator iter = elements.iterator(); while(iter.hasNext()) { Element element = iter.next(); processor.processElement(element); } finally { readLock.unlock(); } // ... }interface ElementProcessor { void processElement(Element element);}
So, in the example above, the first line in processElements will let everybody play at once so long as the write lock is not held. Then, when the write lock is requested, the requesting thread will be made to wait until all existing readers are done (no further readers will be let in), and then the writer thread may begin.
发表评论
- 浏览: 220236 次
- 性别:

- 来自: 广州

- 详细资料
搜索本博客
我的相册
VB-seamless
共 13 张
共 13 张
最近加入圈子
最新评论
-
使用Terracotta和Tomcat建 ...
renavatior 写道"运行start.bat 9081 这样我们就启动了目 ...
-- by rainsf -
使用Terracotta和Tomcat建 ...
"运行start.bat 9081 这样我们就启动了目录9081中的tomcat ...
-- by renavatior -
广州3年多经验 5500的 ...
fucku 写道广州的软件厂家可比深圳多多了,不过比起北京上海来,还是少了很多, ...
-- by yongfan_420 -
广州3年多经验 5500的 ...
广州的软件厂家可比深圳多多了,不过比起北京上海来,还是少了很多,导致机会也没有这 ...
-- by fucku -
广州3年多经验 5500的 ...
想高工资就去厂家咯,老在集成商里面混能有多大个奔头
-- by fucku






评论排行榜