1 /*
  2  * Copyright (c) 2025, 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  * @bug 8369227
 27  * @summary Stress test untimed park after a timed park when a thread is unparked around the
 28  *     same time that timeout expires.
 29  * @run main ParkAfterTimedPark 200
 30  */
 31 
 32 import java.time.Instant;
 33 import java.util.concurrent.CountDownLatch;
 34 import java.util.concurrent.ThreadLocalRandom;
 35 import java.util.concurrent.TimeUnit;
 36 import java.util.concurrent.locks.LockSupport;
 37 
 38 public class ParkAfterTimedPark {
 39     public static void main(String[] args) throws Exception {
 40         int iterations = (args.length > 0) ? Integer.parseInt(args[0]) : 100;
 41 
 42         for (int i = 1; i <= iterations; i++) {
 43             System.out.println(Instant.now() + " => " + i + " of " + iterations);
 44             for (int timeout = 1; timeout <= 10; timeout++) {
 45                 test(timeout);
 46             }
 47         }
 48     }
 49 
 50     /**
 51      * Creates two virtual threads. The first does a timed-park for the given time,
 52      * then parks in CountDownLatch.await. A second virtual thread unparks the first
 53      * around the same time that the timeout for the first expires.
 54      */
 55     private static void test(int millis) throws Exception {
 56         long nanos = TimeUnit.MILLISECONDS.toNanos(millis);
 57 
 58         var finish = new CountDownLatch(1);
 59 
 60         Thread thread1 = Thread.startVirtualThread(() -> {
 61             LockSupport.parkNanos(nanos);
 62             boolean done = false;
 63             while (!done) {
 64                 try {
 65                     finish.await();
 66                     done = true;
 67                 } catch (InterruptedException e) { }
 68             }
 69         });
 70 
 71         Thread thread2 = Thread.startVirtualThread(() -> {
 72             int delta = ThreadLocalRandom.current().nextInt(millis);
 73             boolean done = false;
 74             while (!done) {
 75                 try {
 76                     Thread.sleep(millis - delta);
 77                     done = true;
 78                 } catch (InterruptedException e) { }
 79             }
 80             LockSupport.unpark(thread1);
 81         });
 82 
 83         // wait for first thread to park before count down
 84         await(thread1, Thread.State.WAITING);
 85         finish.countDown();
 86 
 87         thread1.join();
 88         thread2.join();
 89     }
 90 
 91     /**
 92      * Waits for the given thread to reach a given state.
 93      */
 94     private static void await(Thread thread, Thread.State expectedState) throws Exception {
 95         Thread.State state = thread.getState();
 96         while (state != expectedState) {
 97              if (state == Thread.State.TERMINATED)
 98                  throw new RuntimeException("Thread has terminated");
 99             Thread.sleep(10);
100             state = thread.getState();
101         }
102     }
103 }