Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/shenandoah/jni/TestJNIGlobalRefs.java
32285 views
/*1* Copyright (c) 2018, Red Hat, Inc. All rights reserved.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation.6*7* This code is distributed in the hope that it will be useful, but WITHOUT8* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or9* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License10* version 2 for more details (a copy is included in the LICENSE file that11* accompanied this code).12*13* You should have received a copy of the GNU General Public License version14* 2 along with this work; if not, write to the Free Software Foundation,15* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.16*17* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA18* or visit www.oracle.com if you need additional information or have any19* questions.20*21*/2223import java.util.Arrays;24import java.util.Random;2526public class TestJNIGlobalRefs {27static {28System.loadLibrary("TestJNIGlobalRefs");29}3031private static final int TIME_MSEC = 120000;32private static final int ARRAY_SIZE = 10000;3334private static native void makeGlobalRef(Object o);35private static native void makeWeakGlobalRef(Object o);36private static native Object readGlobalRef();37private static native Object readWeakGlobalRef();3839public static void main(String[] args) throws Throwable {40seedGlobalRef();41seedWeakGlobalRef();42long start = System.currentTimeMillis();43long current = start;44while (current - start < TIME_MSEC) {45testGlobal();46testWeakGlobal();47Thread.sleep(1);48current = System.currentTimeMillis();49}50}5152private static void seedGlobalRef() {53int[] a = new int[ARRAY_SIZE];54fillArray(a, 1337);55makeGlobalRef(a);56}5758private static void seedWeakGlobalRef() {59int[] a = new int[ARRAY_SIZE];60fillArray(a, 8080);61makeWeakGlobalRef(a);62}6364private static void testGlobal() {65int[] a = (int[]) readGlobalRef();66checkArray(a, 1337);67}6869private static void testWeakGlobal() {70int[] a = (int[]) readWeakGlobalRef();71if (a != null) {72checkArray(a, 8080);73} else {74// weak reference is cleaned, recreate:75seedWeakGlobalRef();76}77}7879private static void fillArray(int[] array, int seed) {80Random r = new Random(seed);81for (int i = 0; i < ARRAY_SIZE; i++) {82array[i] = r.nextInt();83}84}8586private static void checkArray(int[] array, int seed) {87Random r = new Random(seed);88if (array.length != ARRAY_SIZE) {89throw new IllegalStateException("Illegal array length: " + array.length + ", but expected " + ARRAY_SIZE);90}91for (int i = 0; i < ARRAY_SIZE; i++) {92int actual = array[i];93int expected = r.nextInt();94if (actual != expected) {95throw new IllegalStateException("Incorrect array data: " + actual + ", but expected " + expected);96}97}98}99}100101102