threadsafeiterator.java
来自「这个是perst-269.zip下面的SOURCECODE,和大家分享了。」· Java 代码 · 共 70 行
JAVA
70 行
package org.garret.perst;
/**
* This class is used to make it possible to iterate collection without locking it and
* when cokkection can be currently updated by other threads.
* Please notice that if other threads are deleting elements from the tree, then
* deleting current elemet of the iterator can still cause the problem.
* Also this iterator should be used only inside one thread - sharing iterator between multiple
* threads will not work correctly.
*/
public class ThreadSafeIterator extends Iterator {
private IResource collection;
private Iterator iterator;
private Object next;
private int nextOid;
public boolean hasNext() {
boolean result;
if (next == null) {
collection.sharedLock();
if (iterator.hasNext()) {
next = iterator.next();
result = true;
} else {
result = false;
}
collection.unlock();
} else {
result = true;
}
return result;
}
public int nextOid() {
int oid = nextOid;
if (oid == 0) {
collection.sharedLock();
oid = iterator.nextOid();
collection.unlock();
} else {
nextOid = 0;
}
return oid;
}
public Object next() {
Object obj = next;
if (obj == null) {
collection.sharedLock();
obj = iterator.next();
collection.unlock();
} else {
next = null;
}
return obj;
}
public ThreadSafeIterator(IResource collection, Iterator iterator) {
this.collection = collection;
this.iterator = iterator;
}
public void remove() {
collection.exclusiveLock();
iterator.remove();
collection.unlock();
next = null;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?