1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.
7  *
8  * This code is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * version 2 for more details (a copy is included in the LICENSE file that
12  * accompanied this code).
13  *
14  * You should have received a copy of the GNU General Public License version
15  * 2 along with this work; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19  * or visit www.oracle.com if you need additional information or have any
20  * questions.
21  */
22 
23 /*
24  * This file is available under and governed by the GNU General Public
25  * License version 2 only, as published by the Free Software Foundation.
26  * However, the following notice accompanied the original version of this
27  * file:
28  *
29  * Written by Martin Buchholz with assistance from members of JCP
30  * JSR-166 Expert Group and released to the public domain, as
31  * explained at http://creativecommons.org/publicdomain/zero/1.0/
32  */
33 
34 /*
35  * @test
36  * @bug 8073704
37  * @summary Checks that once isDone() returns true,
38  * get() never throws InterruptedException or TimeoutException
39  * @library /test/lib
40  */
41 
42 import static java.util.concurrent.TimeUnit.MILLISECONDS;
43 
44 import java.util.ArrayList;
45 import java.util.concurrent.Callable;
46 import java.util.concurrent.CountDownLatch;
47 import java.util.concurrent.ExecutorService;
48 import java.util.concurrent.Executors;
49 import java.util.concurrent.Future;
50 import java.util.concurrent.FutureTask;
51 import java.util.concurrent.ThreadLocalRandom;
52 import java.util.concurrent.TimeUnit;
53 import java.util.concurrent.atomic.AtomicBoolean;
54 import java.util.concurrent.atomic.AtomicReference;
55 import jdk.test.lib.Utils;
56 
57 public class DoneMeansDone {
58     static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
59 
main(String[] args)60     public static void main(String[] args) throws Throwable {
61         final int iters = 1000;
62         final int nThreads = 2;
63         final AtomicBoolean done = new AtomicBoolean(false);
64         final AtomicReference<FutureTask<Boolean>> a = new AtomicReference<>();
65         final CountDownLatch threadsStarted = new CountDownLatch(nThreads);
66         final Callable<Boolean> alwaysTrue = new Callable<>() {
67             public Boolean call() {
68                 return true;
69             }};
70 
71         final Runnable observer = new Runnable() { public void run() {
72             threadsStarted.countDown();
73             final ThreadLocalRandom rnd = ThreadLocalRandom.current();
74             try {
75                 for (FutureTask<Boolean> f; !done.get();) {
76                     f = a.get();
77                     if (f != null) {
78                         do {} while (!f.isDone());
79                         Thread.currentThread().interrupt();
80                         if (!(rnd.nextBoolean()
81                               ? f.get()
82                               : f.get(-1L, TimeUnit.DAYS)))
83                             throw new AssertionError();
84                     }
85                 }
86             } catch (Exception t) { throw new AssertionError(t); }
87         }};
88 
89         final ArrayList<Future<?>> futures = new ArrayList<>(nThreads);
90         final ExecutorService pool = Executors.newCachedThreadPool();
91         for (int i = 0; i < nThreads; i++)
92             futures.add(pool.submit(observer));
93         threadsStarted.await();
94         for (int i = 0; i < iters; i++) {
95             FutureTask<Boolean> f = new FutureTask<>(alwaysTrue);
96             a.set(f);
97             f.run();
98         }
99         done.set(true);
100         pool.shutdown();
101         if (!pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS))
102             throw new AssertionError();
103         for (Future<?> future : futures)
104             future.get();
105     }
106 }
107