Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions core/src/main/java/fj/P1.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,12 @@ public A _1() {
A a = v != null ? v.get() : null;
if (a == null)
synchronized (latch) {
if (v == null || v.get() == null)
if (v == null || v.get() == null) {
a = self._1();
v = new SoftReference<A>(a);
v = new SoftReference<A>(a);
} else {
a = v.get();
}
}
return a;
}
Expand Down
31 changes: 31 additions & 0 deletions core/src/test/java/fj/P1Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package fj;

import org.junit.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public final class P1Test {

@Test
public void bug105() throws Exception {
final P1<String> p1 = P.p("Foo").memo();
final AtomicInteger nullCounter = new AtomicInteger();
ExecutorService executorService = Executors.newCachedThreadPool();

for (int i = 0; i < 10000; i++) {
executorService.submit(() -> {
if (p1._1() == null) {
nullCounter.incrementAndGet();
}
});
}

executorService.shutdown();
executorService.awaitTermination(10, TimeUnit.DAYS);

org.junit.Assert.assertEquals("Race condition in P1.memo()", 0, nullCounter.get());
}
}