java.util.ConcurrentModificationException当我LinkedHashMap在下面的代码中迭代结构时,不确定触发的是什么.使用这种Map.Entry方法很好.没有从之前的帖子中得到关于触发这个问题的一个很好的解释.
任何帮助,将不胜感激.
import java.util.LinkedHashMap;
import java.util.Map;
public class LRU {
// private Map<String,Integer> m = new HashMap<String,Integer>();
// private SortedMap<String,Integer> lru_cache = Collections.synchronizedSortedMap(new TreeMap<String, Integer>());
private static final int MAX_SIZE = 3;
private LinkedHashMap<String,Integer> lru_cache = new LinkedHashMap<String,Integer>(MAX_SIZE, 0.1F, true){
@Override
protected boolean removeEldestEntry(Map.Entry eldest) {
return(lru_cache.size() > MAX_SIZE);
}
};
public Integer get1(String s){
return lru_cache.get(s);
}
public void displayMap(){
/**
* Exception in thread "main" java.util.ConcurrentModificationException
at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(LinkedHashMap.java:373)
at java.util.LinkedHashMap$KeyIterator.next(LinkedHashMap.java:384)
at LRU.displayMap(LRU.java:23)
at …Run Code Online (Sandbox Code Playgroud) 我正在调用返回TreeMap实例的函数,并在调用代码中我想修改TreeMap.但是,我得到了一个ConcurrentModificationException.
这是我的代码:
public Map<String, String> function1() {
Map<String, String> key_values = Collections.synchronizedMap(new TreeMap<String, String>());
// all key_values.put() goes here
return key_values;
}
Run Code Online (Sandbox Code Playgroud)
我的调用代码是:
Map<String, String> key_values =Collections.synchronizedMap(Classname.function1());
//here key_values.put() giving ConcurrentModificationException
Run Code Online (Sandbox Code Playgroud) 如何更新AtomicInteger当前值是否小于给定值?这个想法是:
AtomicInteger ai = new AtomicInteger(0);
...
ai.update(threadInt); // this call happens concurrently
...
// inside AtomicInteger atomic operation
synchronized {
if (ai.currentvalue < threadInt)
ai.currentvalue = threadInt;
}
Run Code Online (Sandbox Code Playgroud) 我在编写旅行推销员计划时遇到过这种情况.对于内循环,我尝试了一个
for(Point x:ArrayList<Point>) {
// modify the iterator
}
Run Code Online (Sandbox Code Playgroud)
但是当向该列表添加另一个点时会导致ConcurrentModicationException被抛出.
但是,当我将循环更改为
for(int x=0; x<ArrayList<Point>.size(); x++) {
// modify the array
}
Run Code Online (Sandbox Code Playgroud)
循环运行良好,没有抛出异常.
两个for循环,那么为什么一个抛出异常而另一个没有呢?
情况:我有一个自定义对象TreeSet,我也使用了自定义比较器.我已经创建了一个在这个TreeSet上使用的迭代器.
TreeSet<Custom> ts=new TreeSet<Custom>();
Iterator<Custom> itr=ts.iterator();
while(itr.hasNext()){
Custom c=itr.next();
//Code to add a new element to the TreeSet ts
}
Run Code Online (Sandbox Code Playgroud)
问题:我想知道如果我在while循环中向TreeSet添加一个新元素,那么新元素会立即被排序.换句话说,如果我在while循环中添加一个新元素并且它小于我当前在c中保存的元素,那么在下一次迭代中我将获得与上一次迭代中相同的元素吗?(因为在排序之后,新添加的元素将占据当前元素之前的某个位置.
final Multimap<Term, BooleanClause> terms = getTerms(bq);
for (Term t : terms.keySet()) {
Collection<BooleanClause> C = new HashSet(terms.get(t));
if (!C.isEmpty()) {
for (Iterator<BooleanClause> it = C.iterator(); it.hasNext();) {
BooleanClause c = it.next();
if(c.isSomething()) C.remove(c);
}
}
}
Run Code Online (Sandbox Code Playgroud)
不是SSCCE,但你能闻到气味吗?
在使用Typesafe Config的Scala应用程序中,我想添加在运行时重新加载Config的可能性.Config实例是不可变的.这是我到目前为止:
package config
trait Settings {
private[config] var config: Config = ConfigFactory.empty()
def engine: EngineSettings
}
trait EngineSettings {
def weight: Int
def offset: Int
}
class AppSettings {
override def engine = new EngineSettings {
override def weight = config.getInt("engine.weight")
override def offset = config.getInt("engine.offset")
}
}
object Settings {
private val namedSettings = new TrieMap[String, AppSettings]
def load(configName: String = "local"): Settings = {
// load config
// create or update AppSettings
// add to map and …Run Code Online (Sandbox Code Playgroud) 我不明白为什么在迭代这个时我得到一个ConcurrentModificationException multimap.我读了以下条目,但我不确定我是否理解了整个事情.我试图添加一个synchronized块.但我怀疑的是与什么同步,何时.
这multimap是一个字段,创建如下:
private Multimap<GenericEvent, Command> eventMultiMap =
Multimaps.synchronizedMultimap(HashMultimap.<GenericEvent, Command> create());
Run Code Online (Sandbox Code Playgroud)
并像这样使用:
eventMultiMap.put(event, command);
Run Code Online (Sandbox Code Playgroud)
并且像这样(我试图在地图上同步这部分,但没有成功)
for (Entry<GenericEvent, Command> entry : eventMultiMap.entries()) {
if (entry.getValue().equals(command)) {
eventMultiMap.remove(entry.getKey(), entry.getValue());
nbRemoved++;
}
}
Run Code Online (Sandbox Code Playgroud) 我想运行模拟用户在同一时间Grails应用程序修改某些数据进行测试.
是否有任何插件/工具/机制可以用来有效地完成这项工作?它们不一定是特定的grails.应该可以并行触发多个动作.
我更喜欢在功能级别上运行测试(到目前为止,我正在使用Selenium进行其他测试)以从用户角度查看结果.当然,这除了可以集成测试完成,如果你会推荐上运行集成度并发修改测试以及.
testing grails functional-testing optimistic-locking concurrentmodification
我在下面有这个代码,我通过执行以下行得到一个ConcurrentModificationException:
filterCardsToDevice(getCollection());
Run Code Online (Sandbox Code Playgroud)
代码:
private List<MyClass> filterCardsToDevice(Collection<MyClass> col) {
final List<MyClass> newList = new ArrayList<MyClass>();
for (MyClass myObj : col) {
long id = myObj.getId();
if (id < 0 || id > 0xFFFFFFFFl) {
// just a log here
} else {
newList.add(myObj);
}
}
return newList;
}
private final Map<Long, MyClass> map = new HashMap<Long, MyClass>();
public Collection<MyClass> getCollection() {
synchronized (map) {
return Collections.unmodifiableCollection(map.values());
}
}
Run Code Online (Sandbox Code Playgroud)
堆栈是:
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:841)
at java.util.HashMap$ValueIterator.next(HashMap.java:871)
at java.util.Collections$UnmodifiableCollection$1.next(Collections.java:1010)
Run Code Online (Sandbox Code Playgroud)
正好在foreach线上:
for (MyClass myObj : col) …Run Code Online (Sandbox Code Playgroud) java ×9
collections ×3
guava ×2
iterator ×2
multimap ×2
concurrency ×1
grails ×1
scala ×1
testing ×1
treemap ×1
treeset ×1
unmodifiable ×1