< prev index next >

test/jdk/java/lang/Thread/virtual/stress/PingPong.java

Print this page

  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         }

 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> {

  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         }

 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> {
< prev index next >