1 /* 2 * Copyright (c) 2020, 2024, 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 CompletableFutureTimedGet 100000 29 */ 30 31 import java.time.Duration; 32 import java.time.Instant; 33 import java.util.ArrayList; 34 import java.util.List; 35 import java.util.concurrent.CompletableFuture; 36 import java.util.concurrent.TimeUnit; 37 import java.util.concurrent.atomic.AtomicInteger; 38 39 public class CompletableFutureTimedGet { 40 41 static final String RESULT = "foo"; 42 43 public static void main(String... args) throws InterruptedException { 44 int threadCount = 250_000; 45 if (args.length > 0) { 46 threadCount = Integer.parseInt(args[0]); 47 } 48 49 // the count of the number of threads that complete successfully 50 AtomicInteger completed = new AtomicInteger(); 51 52 // list of futures and threads 53 List<CompletableFuture<String>> futures = new ArrayList<>(); 54 List<Thread> threads = new ArrayList<>(); 55 56 // start threads that wait with timeout for a result 57 for (int i = 0; i < threadCount; i++) { 58 var future = new CompletableFuture<String>(); 59 futures.add(future); 60 61 // start a thread that uses a timed-get to wait for the result 62 Thread thread = Thread.ofVirtual().start(() -> { 63 try { 64 String result = future.get(1, TimeUnit.DAYS); 65 if (!RESULT.equals(result)) { 66 throw new RuntimeException("result=" + result); 67 } 68 completed.incrementAndGet(); 69 } catch (Exception e) { 70 e.printStackTrace(); 71 } 72 }); 73 threads.add(thread); 74 } 75 76 // sets the result, which will unpark waiting threads 77 futures.forEach(f -> f.complete(RESULT)); 78 79 // wait for all threads to terminate 80 long lastTimestamp = System.currentTimeMillis(); 81 boolean done; 82 do { 83 done = true; 84 for (Thread t : threads) { 85 if (!t.join(Duration.ofSeconds(1))) { 86 done = false; 87 } 88 } 89 90 // print trace message so the output tracks progress 91 long currentTime = System.currentTimeMillis(); 92 if (done || ((currentTime - lastTimestamp) > 500)) { 93 System.out.format("%s => completed %d of %d%n", 94 Instant.now(), completed.get(), threadCount); 95 lastTimestamp = currentTime; 96 } 97 98 } while (!done); 99 100 // all tasks should have completed successfully 101 int completedCount = completed.get(); 102 if (completedCount != threadCount) { 103 throw new RuntimeException("completed = " + completedCount); 104 } 105 } 106 }