Fixed synchronization with double lock
@@ -34,7 +34,8 @@ public abstract class SpectralDBParser {
protected final int bufferEntries;
// process entries
protected final LibraryEntryProcessor processor;
- private final List<SpectralDBEntry> list;
+ protected final Object LOCK = new Object();
+ private List<SpectralDBEntry> list;
private int processedEntries = 0;
public SpectralDBParser(int bufferEntries, LibraryEntryProcessor processor) {
@@ -64,6 +65,8 @@ public abstract class SpectralDBParser {
.anyMatch(v -> Double.compare(v, 0) == 0)) {
return false;
}
+ synchronized (LOCK) {
+ // need double lock as list changes inside
synchronized (list) {
list.add(entry);
if (list.size() % bufferEntries == 0) {
@@ -72,10 +75,11 @@ public abstract class SpectralDBParser {
processor.processNextEntries(list, processedEntries);
processedEntries += list.size();
// new list
- list.clear();
+ list = new ArrayList<>();
+ }
}
- return true;
}
+ return true;
}
/**
@@ -83,12 +87,14 @@ public abstract class SpectralDBParser {
*/
protected void finish() {
// push entries
+ synchronized (LOCK) {
synchronized (list) {
if (!list.isEmpty()) {
logger.info("Imported last " + list.size() + " library entries");
processor.processNextEntries(list, processedEntries);
processedEntries += list.size();
- list.clear();
+ list = new ArrayList<>();
+ }
}
}
logger.info(processedEntries + " library entries imported");
