Skip to content

Commit 14f8458

Browse files
ToMany: ensure double-checked locking works (e.g. use volatile).
Also performance improvement by only reading listFactory field once in most cases.
1 parent abbba7b commit 14f8458

File tree

1 file changed

+8
-6
lines changed
  • objectbox-java/src/main/java/io/objectbox/relation

1 file changed

+8
-6
lines changed

objectbox-java/src/main/java/io/objectbox/relation/ToMany.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ public class ToMany<TARGET> implements List<TARGET>, Serializable {
6868
private final Object entity;
6969
private final RelationInfo<Object, TARGET> relationInfo;
7070

71-
private ListFactory listFactory;
71+
private volatile ListFactory listFactory;
7272
private List<TARGET> entities;
7373

7474
/** Counts of all entities in the list ({@link #entities}). */
7575
private Map<TARGET, Integer> entityCounts;
7676

7777
/** Entities added since last put/sync. Map is used as a set (value is always Boolean.TRUE). */
78-
private Map<TARGET, Boolean> entitiesAdded;
78+
private volatile Map<TARGET, Boolean> entitiesAdded;
7979

8080
/** Entities removed since last put/sync. Map is used as a set (value is always Boolean.TRUE). */
8181
private Map<TARGET, Boolean> entitiesRemoved;
@@ -129,14 +129,16 @@ public synchronized void setRemoveFromTargetBox(boolean removeFromTargetBox) {
129129
}
130130

131131
public ListFactory getListFactory() {
132-
if (listFactory == null) {
132+
ListFactory result = listFactory;
133+
if (result == null) {
133134
synchronized (this) {
134-
if (listFactory == null) {
135-
listFactory = new CopyOnWriteArrayListFactory();
135+
result = listFactory;
136+
if (result == null) {
137+
listFactory = result = new CopyOnWriteArrayListFactory();
136138
}
137139
}
138140
}
139-
return listFactory;
141+
return result;
140142
}
141143

142144
private void ensureBoxes() {

0 commit comments

Comments
 (0)