1 /*
2  * Copyright (C) 2013 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 public class Main {
18     private static final int TEST_TIME = 5;
19 
main(String[] args)20     public static void main(String[] args) {
21         System.out.println("Running (" + TEST_TIME + " seconds) ...");
22         InfiniteDoWhileLoopWithLong doWhileLoopWithLong = new InfiniteDoWhileLoopWithLong();
23         SimpleLoopThread[] simpleLoops = {
24                 new InfiniteForLoop(),
25                 new InfiniteWhileLoop(),
26                 new InfiniteWhileLoopWithIntrinsic(),
27                 new InfiniteDoWhileLoop(),
28                 new MakeGarbage(),
29                 new InfiniteWhileLoopWithSpecialReturnArgOrConst(new SpecialMethods1()),
30                 new InfiniteWhileLoopWithSpecialReturnArgOrConst(new SpecialMethods2()),
31                 new InfiniteWhileLoopWithSpecialPutOrNop(new SpecialMethods1()),
32                 new InfiniteWhileLoopWithSpecialPutOrNop(new SpecialMethods2()),
33                 new InfiniteWhileLoopWithSpecialConstOrIGet(new SpecialMethods1()),
34                 new InfiniteWhileLoopWithSpecialConstOrIGet(new SpecialMethods2()),
35                 new InfiniteWhileLoopWithSpecialConstOrIGetInTryCatch(new SpecialMethods1()),
36                 new InfiniteWhileLoopWithSpecialConstOrIGetInTryCatch(new SpecialMethods2()),
37         };
38         doWhileLoopWithLong.start();
39         for (SimpleLoopThread loop : simpleLoops) {
40             loop.start();
41         }
42         for (int i = 0; i < TEST_TIME; i++) {
43           Runtime.getRuntime().gc();
44           System.out.println(".");
45           sleep(1000);
46         }
47         doWhileLoopWithLong.stopNow();
48         for (SimpleLoopThread loop : simpleLoops) {
49             loop.stopNow();
50         }
51         System.out.println("Done.");
52     }
53 
sleep(int ms)54     public static void sleep(int ms) {
55         try {
56             Thread.sleep(ms);
57         } catch (InterruptedException ie) {
58             System.out.println("sleep was interrupted");
59         }
60     }
61 }
62 
63 class SimpleLoopThread extends Thread {
64   volatile protected boolean keepGoing = true;
stopNow()65   public void stopNow() {
66     keepGoing = false;
67   }
68 }
69 
70 interface SpecialMethodInterface {
ReturnArgOrConst(long arg)71   long ReturnArgOrConst(long arg);
PutOrNop(long arg)72   void PutOrNop(long arg);
ConstOrIGet()73   long ConstOrIGet();
74 }
75 
76 class SpecialMethods1 implements SpecialMethodInterface {
ReturnArgOrConst(long arg)77   public long ReturnArgOrConst(long arg) {
78     return 42L;
79   }
PutOrNop(long arg)80   public void PutOrNop(long arg) {
81   }
ConstOrIGet()82   public long ConstOrIGet() {
83     return 42L;
84   }
85 }
86 
87 class SpecialMethods2 implements SpecialMethodInterface {
88   public long value = 42L;
ReturnArgOrConst(long arg)89   public long ReturnArgOrConst(long arg) {
90     return arg;
91   }
PutOrNop(long arg)92   public void PutOrNop(long arg) {
93     value = arg;
94   }
ConstOrIGet()95   public long ConstOrIGet() {
96     return value;
97   }
98 }
99 
100 class InfiniteWhileLoopWithSpecialReturnArgOrConst extends SimpleLoopThread {
101   private SpecialMethodInterface smi;
InfiniteWhileLoopWithSpecialReturnArgOrConst(SpecialMethodInterface smi)102   public InfiniteWhileLoopWithSpecialReturnArgOrConst(SpecialMethodInterface smi) {
103     this.smi = smi;
104   }
run()105   public void run() {
106     long i = 0L;
107     while (keepGoing) {
108       i += smi.ReturnArgOrConst(i);
109     }
110   }
111 }
112 
113 class InfiniteWhileLoopWithSpecialPutOrNop extends SimpleLoopThread {
114   private SpecialMethodInterface smi;
InfiniteWhileLoopWithSpecialPutOrNop(SpecialMethodInterface smi)115   public InfiniteWhileLoopWithSpecialPutOrNop(SpecialMethodInterface smi) {
116     this.smi = smi;
117   }
run()118   public void run() {
119     long i = 0L;
120     while (keepGoing) {
121       smi.PutOrNop(i);
122       i++;
123     }
124   }
125 }
126 
127 class InfiniteWhileLoopWithSpecialConstOrIGet extends SimpleLoopThread {
128   private SpecialMethodInterface smi;
InfiniteWhileLoopWithSpecialConstOrIGet(SpecialMethodInterface smi)129   public InfiniteWhileLoopWithSpecialConstOrIGet(SpecialMethodInterface smi) {
130     this.smi = smi;
131   }
run()132   public void run() {
133     long i = 0L;
134     while (keepGoing) {
135       i += smi.ConstOrIGet();
136     }
137   }
138 }
139 
140 class InfiniteWhileLoopWithSpecialConstOrIGetInTryCatch extends SimpleLoopThread {
141   private SpecialMethodInterface smi;
InfiniteWhileLoopWithSpecialConstOrIGetInTryCatch(SpecialMethodInterface smi)142   public InfiniteWhileLoopWithSpecialConstOrIGetInTryCatch(SpecialMethodInterface smi) {
143     this.smi = smi;
144   }
run()145   public void run() {
146     try {
147       long i = 0L;
148       while (keepGoing) {
149         i += smi.ConstOrIGet();
150       }
151     } catch (Throwable ignored) { }
152   }
153 }
154 
155 class InfiniteWhileLoopWithIntrinsic extends SimpleLoopThread {
156   private String[] strings = { "a", "b", "c", "d" };
157   private int sum = 0;
run()158   public void run() {
159     int i = 0;
160     while (keepGoing) {
161       i++;
162       sum += strings[i & 3].length();
163     }
164   }
165 }
166 
167 class InfiniteDoWhileLoopWithLong extends Thread {
168   volatile private long keepGoing = 7L;
run()169   public void run() {
170     int i = 0;
171     do {
172       i++;
173     } while (keepGoing >= 4L);
174   }
stopNow()175   public void stopNow() {
176     keepGoing = 1L;
177   }
178 }
179 
180 class InfiniteWhileLoop extends SimpleLoopThread {
run()181   public void run() {
182     int i = 0;
183     while (keepGoing) {
184       i++;
185     }
186   }
187 }
188 
189 class InfiniteDoWhileLoop extends SimpleLoopThread {
run()190   public void run() {
191     int i = 0;
192     do {
193       i++;
194     } while (keepGoing);
195   }
196 }
197 
198 class InfiniteForLoop extends SimpleLoopThread {
run()199   public void run() {
200     int i = 0;
201     for (int j = 0; keepGoing; j++) {
202       i += j;
203     }
204   }
205 }
206 
207 class MakeGarbage extends SimpleLoopThread {
run()208   public void run() {
209     while (keepGoing) {
210       byte[] garbage = new byte[100000];
211     }
212   }
213 }
214