src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/HelloWorld.java
author jjg
Mon, 20 May 2019 10:57:57 -0700
changeset 54950 46ae54c3026d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8223663: Update links for tool guides Reviewed-by: alanb, erikj, darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
9127
c3fc6f9ed68d 7010849: 5/5 Extraneous javac source/target options when building sa-jdi
andrew
parents: 5547
diff changeset
     2
 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
package sun.jvm.hotspot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import java.lang.reflect.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
public class HelloWorld {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
  private static String helloWorldString = "Hello, world!";
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
  private static volatile int helloWorldTrigger = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  private static final boolean useMethodInvoke = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
  private static Object lock = new Object();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  public static void main(String[] args) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
    int foo = a();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
    System.out.println("HelloWorld exiting. a() = " + foo);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  private static int a() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
    return 1 + b();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  private static int b() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
    return 1 + c();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  private static int c() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
    return 1 + d("Hi");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  private static int d(String x) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
    System.out.println("HelloWorld.d() received \"" + x + "\" as argument");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
    synchronized(lock) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
      if (useMethodInvoke) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
        try {
9127
c3fc6f9ed68d 7010849: 5/5 Extraneous javac source/target options when building sa-jdi
andrew
parents: 5547
diff changeset
    58
          Method method = HelloWorld.class.getMethod("e");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
          Integer result = (Integer) method.invoke(null, new Object[0]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
          return result.intValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
        catch (Exception e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
          throw new RuntimeException(e.toString());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
        int i = fib(10); // 89
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
        long l = i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
        float f = i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
        double d = i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
        char c = (char) i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
        short s = (short) i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
        byte b = (byte) i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
        int ret = e();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
        System.out.println("Tenth Fibonacci number in all formats: " +
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
                           i + ", " +
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
                           l + ", " +
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
                           f + ", " +
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
                           d + ", " +
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
                           c + ", " +
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
                           s + ", " +
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
                           b);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
        return ret;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  public static int e() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
    System.out.println("Going to sleep...");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
    int i = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
    while (helloWorldTrigger == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
      if (++i == 1000000) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
        System.gc();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
    System.out.println(helloWorldString);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    while (helloWorldTrigger != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
    return i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  // Tree-recursive implementation for test
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  public static int fib(int n) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
    if (n < 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
      return 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
    return fib(n - 1) + fib(n - 2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
}