Elliott Hughes | e536c64 | 2012-06-18 16:55:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /** |
| 18 | * Running concurrent gc and doing some System.arraycopy |
| 19 | * Several threads is created in order to increase the probability |
| 20 | * of thread switches at critical points. Without creating several |
| 21 | * threads the test case usually passed even when there were bugs. |
| 22 | * Size of array and amount of garbage created is based on experimental |
| 23 | * numbers and is a tradeoff between time that the test takes when |
| 24 | * it succeeds and the probability that the test discovers a problem. |
| 25 | */ |
| 26 | public class Main { |
| 27 | public static void main(String args[]) { |
| 28 | new ObjectCreatorThread(true).start(); |
| 29 | new ObjectCreatorThread(false).start(); |
| 30 | new ObjectCreatorThread(false).start(); |
| 31 | } |
| 32 | |
| 33 | static class ObjectCreatorThread extends Thread { |
| 34 | boolean mDoLog; |
| 35 | public ObjectCreatorThread(boolean doLog) { |
| 36 | mDoLog = doLog; |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public void run() { |
| 41 | new Main().stressArray(mDoLog); |
| 42 | } |
| 43 | } |
| 44 | |
Hans Boehm | 6972b96 | 2020-02-24 16:00:13 -0800 | [diff] [blame] | 45 | Object [] array = new Object[8000]; |
Elliott Hughes | e536c64 | 2012-06-18 16:55:54 -0700 | [diff] [blame] | 46 | |
Lokesh Gidra | a48f6f1 | 2020-09-23 09:36:01 -0700 | [diff] [blame] | 47 | void allocateFourStrings() { |
| 48 | for (int i = 0; i < 4; i++) { |
| 49 | String str = new String("Creating some garbage" + Math.random()); |
| 50 | if (str.length() < 22) { |
| 51 | System.out.println("bad length"); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
Elliott Hughes | e536c64 | 2012-06-18 16:55:54 -0700 | [diff] [blame] | 56 | void stressArray(boolean doLog) { |
| 57 | // We want many references in the array |
| 58 | // We also want elements close to each other to have large |
| 59 | // diff in address so lets skip every 2:nd address so it is null |
| 60 | if (doLog) { |
| 61 | System.out.println("Initializing..."); |
| 62 | } |
| 63 | for (int i = 0; i < array.length; i+=2) { |
| 64 | array[i] = new String("Creating some garbage" + i); |
| 65 | } |
| 66 | |
| 67 | if (doLog) { |
| 68 | System.out.println("Starting the test"); |
| 69 | } |
| 70 | |
| 71 | for (int j = 0; j < array.length; j++) { |
| 72 | Object obj = array[array.length - 1]; |
| 73 | System.arraycopy(array, 0, array, 1, array.length - 1); |
| 74 | array[0] = obj; |
Lokesh Gidra | a48f6f1 | 2020-09-23 09:36:01 -0700 | [diff] [blame] | 75 | allocateFourStrings(); |
Elliott Hughes | e536c64 | 2012-06-18 16:55:54 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | for (int j = 0; j < array.length; j++) { |
| 79 | Object obj = array[0]; |
| 80 | System.arraycopy(array, 1, array, 0, array.length - 1); |
| 81 | array[array.length - 1] = obj; |
Lokesh Gidra | a48f6f1 | 2020-09-23 09:36:01 -0700 | [diff] [blame] | 82 | allocateFourStrings(); |
Elliott Hughes | e536c64 | 2012-06-18 16:55:54 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | if (doLog) { |
| 86 | System.out.println("Test OK"); |
| 87 | } |
| 88 | } |
| 89 | } |