1 /*
 2  * Copyright (c) 2018, 2020, 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  * @summary Verifies JVMTI support for VThreads.
27  * @compile --enable-preview -source ${jdk.version} VThreadTest.java
28  * @run main/othervm/native --enable-preview -agentlib:VThreadTest VThreadTest
29  */
30 
31 import java.util.concurrent.*;
32 
33 public class VThreadTest {
34     private static final String agentLib = "VThreadTest";
35 
36     static final int MSG_COUNT = 10*1000;
37     static final SynchronousQueue<String> QUEUE = new SynchronousQueue<>();
38 
39     static void producer(String msg) throws InterruptedException {
40         int ii = 1;
41         long ll = 2*(long)ii;
42         float ff = ll + 1.2f;
43         double dd = ff + 1.3D;
44         msg += dd;
45         QUEUE.put(msg);
46     }
47 
48     static final Runnable PRODUCER = () -> {
49         try {
50             for (int i = 0; i < MSG_COUNT; i++) {
51                 producer("msg: ");
52             }
53         } catch (InterruptedException e) { }
54     };
55 
56     static final Runnable CONSUMER = () -> {
57         try {
58             for (int i = 0; i < MSG_COUNT; i++) {
59                 String s = QUEUE.take();
60             }
61         } catch (InterruptedException e) { }
62     };
63 
64     public static void test1() throws Exception {
65         Thread producer = Thread.ofVirtual().name("VThread-Producer").start(PRODUCER);
66         Thread consumer = Thread.ofVirtual().name("VThread-Consumer").start(CONSUMER);
67         producer.join();
68         consumer.join();
69     }
70 
71     void runTest() throws Exception {
72         test1();
73     }
74 
75     public static void main(String[] args) throws Exception {
76         try {
77             System.loadLibrary(agentLib);
78         } catch (UnsatisfiedLinkError ex) {
79             System.err.println("Failed to load " + agentLib + " lib");
80             System.err.println("java.library.path: " + System.getProperty("java.library.path"));
81             throw ex;
82         }
83 
84         VThreadTest obj = new VThreadTest();
85         obj.runTest();
86     }
87 }