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