1 /*
  2  * Copyright (C) 2021 THL A29 Limited, a Tencent company. 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 Test virtual thread park when scheduler is a fixed thread pool
 27  * @requires vm.continuations
 28  * @modules java.base/java.lang:+open
 29  * @library /test/lib
 30  * @run main ParkWithFixedThreadPool
 31  */
 32 
 33 import java.util.concurrent.Executor;
 34 import java.util.concurrent.ExecutorService;
 35 import java.util.concurrent.Executors;
 36 import java.util.concurrent.ThreadFactory;
 37 import java.util.concurrent.locks.LockSupport;
 38 import jdk.test.lib.thread.VThreadScheduler;
 39 
 40 public class ParkWithFixedThreadPool {
 41     public static void main(String[] args) throws Exception {
 42         try (var scheduler = new Scheduler(8)) {
 43             int vthreadCount = 300;
 44             Thread[] vthreads = new Thread[vthreadCount];
 45             Runnable target = new Runnable() {
 46                 public void run() {
 47                     int myIndex = -1;
 48                     for (int i = 0; i < vthreadCount; i++) {
 49                         if (vthreads[i] == Thread.currentThread()) {
 50                             myIndex = i;
 51                             break;
 52                         }
 53                     }
 54 
 55                     if (myIndex > 0) {
 56                         LockSupport.unpark(vthreads[myIndex - 1]);
 57                     }
 58 
 59                     if (myIndex != (vthreadCount - 1)) {
 60                         LockSupport.park();
 61                     }
 62                 }
 63             };
 64 
 65             ThreadFactory factory = VThreadScheduler.virtualThreadFactory(scheduler);
 66 
 67             for (int i = 0; i < vthreadCount; i++) {
 68                 vthreads[i] = factory.newThread(target);
 69             }
 70             for (int i = 0; i < vthreadCount; i++) {
 71                 vthreads[i].start();
 72             }
 73 
 74             for (int i = 0; i < vthreadCount; i++) {
 75                 vthreads[i].join();
 76             }
 77         }
 78     }
 79 
 80     static class Scheduler implements Executor, AutoCloseable {
 81         private final ExecutorService pool;
 82 
 83         Scheduler(int poolSize) {
 84             pool = Executors.newFixedThreadPool(poolSize);
 85         }
 86 
 87         @Override
 88         public void execute(Runnable task) {
 89             try {
 90                 pool.execute(task);
 91             } finally {
 92                 // ExecutorService::execute may consume parking permit
 93                 LockSupport.unpark(Thread.currentThread());
 94             }
 95         }
 96 
 97         @Override
 98         public void close() {
 99             pool.close();
100         }
101     }
102 }