1 /*
  2  * Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @summary Stress parking with CompletableFuture timed get
 27  * @requires vm.debug != true & vm.continuations
 28  * @run main/othervm -Xmx1g TimedGet 100000
 29  */
 30 
 31 import java.time.Duration;
 32 import java.util.ArrayList;
 33 import java.util.List;
 34 import java.util.concurrent.CompletableFuture;
 35 import java.util.concurrent.TimeUnit;
 36 import java.util.concurrent.atomic.AtomicInteger;
 37 
 38 public class TimedGet {
 39 
 40     static final String RESULT = "foo";
 41 
 42     public static void main(String... args) throws InterruptedException {
 43         int threadCount = 250_000;
 44         if (args.length > 0) {
 45             threadCount = Integer.parseInt(args[0]);
 46         }
 47 
 48         // the count of the number of threads that complete successfully
 49         AtomicInteger completed = new AtomicInteger();
 50 
 51         // list of futures and threads
 52         List<CompletableFuture<String>> futures = new ArrayList<>();
 53         List<Thread> threads = new ArrayList<>();
 54 
 55         // start threads that wait with timeout for a result
 56         for (int i = 0; i < threadCount; i++) {
 57             var future = new CompletableFuture<String>();
 58             futures.add(future);
 59 
 60             // start a thread that uses a timed-get to wait for the result
 61             Thread thread = Thread.ofVirtual().start(() -> {
 62                 try {
 63                     String result = future.get(1, TimeUnit.DAYS);
 64                     if (!RESULT.equals(result)) {
 65                         throw new RuntimeException("result=" + result);
 66                     }
 67                     completed.incrementAndGet();
 68                 } catch (Exception e) {
 69                     e.printStackTrace();
 70                 }
 71             });
 72             threads.add(thread);
 73         }
 74 
 75         // sets the result, which will unpark waiting threads
 76         futures.forEach(f -> f.complete(RESULT));
 77 
 78         // wait for all threads to terminate
 79         long lastTimestamp = System.currentTimeMillis();
 80         int i = 0;
 81         while (i < threadCount) {
 82             Thread t = threads.get(i);
 83             boolean terminated;
 84             if (t.isAlive()) {
 85                 terminated = t.join(Duration.ofMillis(500));
 86 
 87                 // print trace message so the output tracks progress
 88                 long currentTime = System.currentTimeMillis();
 89                 if ((currentTime - lastTimestamp) > 500) {
 90                     System.out.println(completed.get());
 91                     lastTimestamp = currentTime;
 92                 }
 93             } else {
 94                 terminated = true;
 95             }
 96             if (terminated) {
 97                 i++;
 98             }
 99         }
100 
101         // all tasks should have completed successfully
102         int completedCount = completed.get();
103         System.out.println(completedCount);
104         if (completedCount != threadCount) {
105             throw new RuntimeException("completed = " + completedCount);
106         }
107     }
108 }