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 id=sq
 26  * @summary Stress test virtual threads with a SynchronousQueue
 27  * @requires vm.debug != true
 28  * @run main PingPong SQ 500000
 29  */
 30 
 31 /*
 32  * @test id=ltq
 33  * @summary Stress test virtual threads with a LinkedTransferQueue
 34  * @requires vm.debug != true
 35  * @run main PingPong LTQ 500000
 36  */
 37 
 38 import java.time.Duration;
 39 import java.time.Instant;
 40 import java.util.concurrent.SynchronousQueue;
 41 import java.util.concurrent.LinkedTransferQueue;
 42 import java.util.concurrent.atomic.AtomicInteger;
 43 
 44 public class PingPong {
 45 
 46     public static void main(String[] args) throws Exception {
 47         Exchanger<String> queue;
 48         int iterations;
 49         if (args.length == 0) {
 50             queue = new LTQExchanger<>();
 51             iterations = 10_000_000;
 52         } else {
 53             if (args[0].equals("SQ")) {
 54                 queue = new SQExchanger<>();
 55             } else {
 56                 queue = new LTQExchanger<>();
 57             }
 58             iterations = Integer.parseInt(args[1]);
 59         }
 60 
 61         AtomicInteger count1 = new AtomicInteger();
 62         AtomicInteger count2 = new AtomicInteger();
 63 
 64         Thread t1 = Thread.ofVirtual().start(() -> {
 65             try {
 66                 while (count1.incrementAndGet() < iterations) {
 67                     queue.transfer("hello");
 68                     String reply = queue.take();
 69                     if (!"ack".equals(reply)) {
 70                         throw new RuntimeException("reply=" + reply);
 71                     }
 72                 }
 73             } catch (Exception e) {
 74                 e.printStackTrace();
 75             }
 76         });
 77 
 78         Thread t2 = Thread.ofVirtual().start(() -> {
 79             try {
 80                 while (count2.incrementAndGet() < iterations) {
 81                     String message = queue.take();
 82                     if (!"hello".equals(message)) {
 83                         throw new RuntimeException("message=" + message);
 84                     }
 85                     queue.transfer("ack");
 86                 }
 87             } catch (Exception e) {
 88                 e.printStackTrace();
 89             }
 90         });
 91 
 92         boolean terminated;
 93         do {
 94             terminated = t1.join(Duration.ofMillis(500));
 95             if (terminated) {
 96                 terminated = t2.join(Duration.ofMillis(500));
 97             }
 98             System.out.format("%s => T1 %d of %d, T2 %d of %d%n",
 99                     Instant.now(), count1.get(), iterations, count2.get(), iterations);
100         } while (!terminated);
101     }
102 
103     interface Exchanger<E> {
104         void transfer (E value) throws InterruptedException;
105         E take() throws InterruptedException;
106     }
107 
108     static class SQExchanger<E> implements Exchanger<E> {
109         private final SynchronousQueue<E> queue = new SynchronousQueue<>();
110         @Override
111         public void transfer(E value) throws InterruptedException {
112             queue.put(value);
113         }
114         @Override
115         public E take() throws InterruptedException {
116             return queue.take();
117         }
118     }
119 
120     static class LTQExchanger<E> implements Exchanger<E> {
121         private final LinkedTransferQueue<E> queue = new LinkedTransferQueue<>();
122         @Override
123         public void transfer(E value) throws InterruptedException {
124             queue.transfer(value);
125         }
126         @Override
127         public E take() throws InterruptedException {
128             return queue.take();
129         }
130     }
131 }