假设我有两个列表:fooList
, 和 barList
.另外,假设我有两个线程:第一个线程遍历 fooList,如果满足某些条件(条件为真),它会从 fooList 中删除元素并将其添加到 barList。第二个遍历 barList,如果其他条件为真,它从 barList 中删除元素,并将其添加到 fooList。
我的处理方式是:
private static Object sharedLock = new Object();
Thread t1 = new Thread() {
public void run() {
synchronized (sharedLock) {
for (Iterator<String> iterator = fooList.iterator(); iterator.hasNext();) {
String fooElement = iterator.next();
if (condition == true) {
iterator.remove();
barList.add(fooElement);
}
}
}
}
};
Thread t2 = new Thread() {
public void run() {
synchronized (sharedLock) {
for (Iterator<String> iterator = barList.iterator(); iterator.hasNext();) {
String barElement = iterator.next();
if (otherCondition == true) {
iterator.remove();
fooList.add(barElement);
}
}
}
}
};
我想知道的是我处理得当吗?有竞争条件的可能性吗?有没有更好的方法来实现相同的功能?
编辑 看起来正确的实现方法是:
Thread t1 = new Thread() {
public void run() {
for (String fooElement : fooList) {
if (condition == true) {
fooList.remove(fooElement);
barList.add(fooElement);
}
}
}
};
Thread t2 = new Thread() {
public void run() {
for (String barElement : barList) {
if (otherCondition == true) {
barList.remove(barElement);
fooList.add(barElement);
}
}
}
};
其中:fooList
和 barList
类型为 CopyOnWriteArrayList<String>
最佳答案
关于java - 线程安全地从一个列表中删除/添加元素到另一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26461428/