File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -223,9 +223,12 @@ public A _1() {
223223 A a = v != null ? v .get () : null ;
224224 if (a == null )
225225 synchronized (latch ) {
226- if (v == null || v .get () == null )
226+ if (v == null || v .get () == null ) {
227227 a = self ._1 ();
228- v = new SoftReference <A >(a );
228+ v = new SoftReference <A >(a );
229+ } else {
230+ a = v .get ();
231+ }
229232 }
230233 return a ;
231234 }
Original file line number Diff line number Diff line change 1+ package fj ;
2+
3+ import org .junit .Test ;
4+
5+ import java .util .concurrent .ExecutorService ;
6+ import java .util .concurrent .Executors ;
7+ import java .util .concurrent .TimeUnit ;
8+ import java .util .concurrent .atomic .AtomicInteger ;
9+
10+ public final class P1Test {
11+
12+ @ Test
13+ public void bug105 () throws Exception {
14+ final P1 <String > p1 = P .p ("Foo" ).memo ();
15+ final AtomicInteger nullCounter = new AtomicInteger ();
16+ ExecutorService executorService = Executors .newCachedThreadPool ();
17+
18+ for (int i = 0 ; i < 10000 ; i ++) {
19+ executorService .submit (() -> {
20+ if (p1 ._1 () == null ) {
21+ nullCounter .incrementAndGet ();
22+ }
23+ });
24+ }
25+
26+ executorService .shutdown ();
27+ executorService .awaitTermination (10 , TimeUnit .DAYS );
28+
29+ org .junit .Assert .assertEquals ("Race condition in P1.memo()" , 0 , nullCounter .get ());
30+ }
31+ }
You can’t perform that action at this time.
0 commit comments