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[10000];
46 
stressArray(boolean doLog)47     void stressArray(boolean doLog) {
48         // We want many references in the array
49         // We also want elements close to each other to have large
50         // diff in address so lets skip every 2:nd address so it is null
51         if (doLog) {
52             System.out.println("Initializing...");
53         }
54         for (int i = 0; i < array.length; i+=2) {
55             array[i] = new String("Creating some garbage" + i);
56         }
57 
58         if (doLog) {
59             System.out.println("Starting the test");
60         }
61 
62         for (int j = 0; j < array.length; j++) {
63             Object obj = array[array.length - 1];
64             System.arraycopy(array, 0, array, 1, array.length - 1);
65             array[0] = obj;
66             new String("Creating some garbage" + Math.random());
67             new String("Creating some garbage" + Math.random());
68             new String("Creating some garbage" + Math.random());
69             new String("Creating some garbage" + Math.random());
70         }
71 
72         for (int j = 0; j < array.length; j++) {
73             Object obj = array[0];
74             System.arraycopy(array, 1, array, 0, array.length - 1);
75             array[array.length - 1] = obj;
76             new String("Creating some garbage" + Math.random());
77             new String("Creating some garbage" + Math.random());
78             new String("Creating some garbage" + Math.random());
79             new String("Creating some garbage" + Math.random());
80         }
81 
82         if (doLog) {
83             System.out.println("Test OK");
84         }
85     }
86 }
87