1 /* 2 * Copyright (c) 2018, Red Hat, Inc. All rights reserved. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 * 22 */ 23 24 import java.util.Arrays; 25 import java.util.Random; 26 27 public class TestJNIGlobalRefs { 28 static { 29 System.loadLibrary("TestJNIGlobalRefs"); 30 } 31 32 private static final int TIME_MSEC = 120000; 33 private static final int ARRAY_SIZE = 10000; 34 35 private static native void makeGlobalRef(Object o); 36 private static native void makeWeakGlobalRef(Object o); 37 private static native Object readGlobalRef(); 38 private static native Object readWeakGlobalRef(); 39 40 public static void main(String[] args) throws Throwable { 41 seedGlobalRef(); 42 seedWeakGlobalRef(); 43 long start = System.currentTimeMillis(); 44 long current = start; 45 while (current - start < TIME_MSEC) { 46 testGlobal(); 47 testWeakGlobal(); 48 Thread.sleep(1); 49 current = System.currentTimeMillis(); 50 } 51 } 52 53 private static void seedGlobalRef() { 54 int[] a = new int[ARRAY_SIZE]; 55 fillArray(a, 1337); 56 makeGlobalRef(a); 57 } 58 59 private static void seedWeakGlobalRef() { 60 int[] a = new int[ARRAY_SIZE]; 61 fillArray(a, 8080); 62 makeWeakGlobalRef(a); 63 } 64 65 private static void testGlobal() { 66 int[] a = (int[]) readGlobalRef(); 67 checkArray(a, 1337); 68 } 69 70 private static void testWeakGlobal() { 71 int[] a = (int[]) readWeakGlobalRef(); 72 if (a != null) { 73 checkArray(a, 8080); 74 } else { 75 // weak reference is cleaned, recreate: 76 seedWeakGlobalRef(); 77 } 78 } 79 80 private static void fillArray(int[] array, int seed) { 81 Random r = new Random(seed); 82 for (int i = 0; i < ARRAY_SIZE; i++) { 83 array[i] = r.nextInt(); 84 } 85 } 86 87 private static void checkArray(int[] array, int seed) { 88 Random r = new Random(seed); 89 if (array.length != ARRAY_SIZE) { 90 throw new IllegalStateException("Illegal array length: " + array.length + ", but expected " + ARRAY_SIZE); 91 } 92 for (int i = 0; i < ARRAY_SIZE; i++) { 93 int actual = array[i]; 94 int expected = r.nextInt(); 95 if (actual != expected) { 96 throw new IllegalStateException("Incorrect array data: " + actual + ", but expected " + expected); 97 } 98 } 99 } 100 }