xref: /aosp_15_r20/art/test/096-array-copy-concurrent-gc/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
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 {
main(String args[])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;
ObjectCreatorThread(boolean doLog)35         public ObjectCreatorThread(boolean doLog) {
36             mDoLog = doLog;
37         }
38 
39         @Override
run()40         public void run() {
41             new Main().stressArray(mDoLog);
42         }
43     }
44 
45     Object [] array = new Object[8000];
46 
allocateFourStrings()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 
stressArray(boolean doLog)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;
75             allocateFourStrings();
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;
82             allocateFourStrings();
83         }
84 
85         if (doLog) {
86             System.out.println("Test OK");
87         }
88     }
89 }
90