Open ttauchen opened 3 years ago
Found the similar issue as above. I think the problem is one cannot return the same object as in the argument of compute
, e.g. the following code block will enter the dead loop:
HTreeMap<String, List<String>> map = (HTreeMap<String, List<String>>) DBMaker.fileDB("map").make().hashMap("map").createOrOpen();
for (int i = 0; i< 10; i++) {
final int finalI = i;
for (int j = 0; j< 10; j++) {
final int finalJ = j;
map.compute(String.valueOf(finalI), (key, value) -> {
if (value == null) {
value = new ArrayList<>();
}
value.add(String.format("%d_%d", finalI, finalJ));
return value;
});
}
}
It falls into deadloop. Instead, I need to copy the value and then return the modified new copy to make it work.
HTreeMap<String, List<String>> map = (HTreeMap<String, List<String>>) DBMaker.fileDB("map").make().hashMap("map").createOrOpen();
for (int i = 0; i< 10; i++) {
final int finalI = i;
for (int j = 0; j< 10; j++) {
final int finalJ = j;
map.compute(String.valueOf(finalI), (key, value) -> {
final List<String> newValue;
if (value == null) {
newValue = new ArrayList<>();
} else {
newValue = new ArrayList<>(value);
}
newValue.add(String.format("%d_%d", finalI, finalJ));
return newValue;
});
}
}
while the following will work
HTreeMap<String, List<String>> map = (HTreeMap<String, List<String>>) DBMaker.fileDB("map").make().hashMap("map").createOrOpen();
for (int i = 0; i< 10; i++) {
final int finalI = i;
for (int j = 0; j< 10; j++) {
final int finalJ = j;
map.compute(String.valueOf(finalI), (key, value) -> {
final List<String> newValue;
if (value == null) {
newValue = new ArrayList<>();
} else {
newValue = new ArrayList<>(value);
}
newValue.add(String.format("%d_%d", finalI, finalJ));
return newValue;
});
}
}
since this does not happen in ConcurrentHashMap
, I think this is a bug.
Using .compute on a
ConcurrentMap
created withmapdb
will get it stuck in an infinite loop: