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
17public class Main {
18    private static final int TEST_TIME = 5;
19
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
54    public static void sleep(int ms) {
55        try {
56            Thread.sleep(ms);
57        } catch (InterruptedException ie) {
58            System.err.println("sleep was interrupted");
59        }
60    }
61}
62
63class SimpleLoopThread extends Thread {
64  volatile protected boolean keepGoing = true;
65  public void stopNow() {
66    keepGoing = false;
67  }
68}
69
70interface SpecialMethodInterface {
71  long ReturnArgOrConst(long arg);
72  void PutOrNop(long arg);
73  long ConstOrIGet();
74}
75
76class SpecialMethods1 implements SpecialMethodInterface {
77  public long ReturnArgOrConst(long arg) {
78    return 42L;
79  }
80  public void PutOrNop(long arg) {
81  }
82  public long ConstOrIGet() {
83    return 42L;
84  }
85}
86
87class SpecialMethods2 implements SpecialMethodInterface {
88  public long value = 42L;
89  public long ReturnArgOrConst(long arg) {
90    return arg;
91  }
92  public void PutOrNop(long arg) {
93    value = arg;
94  }
95  public long ConstOrIGet() {
96    return value;
97  }
98}
99
100class InfiniteWhileLoopWithSpecialReturnArgOrConst extends SimpleLoopThread {
101  private SpecialMethodInterface smi;
102  public InfiniteWhileLoopWithSpecialReturnArgOrConst(SpecialMethodInterface smi) {
103    this.smi = smi;
104  }
105  public void run() {
106    long i = 0L;
107    while (keepGoing) {
108      i += smi.ReturnArgOrConst(i);
109    }
110  }
111}
112
113class InfiniteWhileLoopWithSpecialPutOrNop extends SimpleLoopThread {
114  private SpecialMethodInterface smi;
115  public InfiniteWhileLoopWithSpecialPutOrNop(SpecialMethodInterface smi) {
116    this.smi = smi;
117  }
118  public void run() {
119    long i = 0L;
120    while (keepGoing) {
121      smi.PutOrNop(i);
122      i++;
123    }
124  }
125}
126
127class InfiniteWhileLoopWithSpecialConstOrIGet extends SimpleLoopThread {
128  private SpecialMethodInterface smi;
129  public InfiniteWhileLoopWithSpecialConstOrIGet(SpecialMethodInterface smi) {
130    this.smi = smi;
131  }
132  public void run() {
133    long i = 0L;
134    while (keepGoing) {
135      i += smi.ConstOrIGet();
136    }
137  }
138}
139
140class InfiniteWhileLoopWithSpecialConstOrIGetInTryCatch extends SimpleLoopThread {
141  private SpecialMethodInterface smi;
142  public InfiniteWhileLoopWithSpecialConstOrIGetInTryCatch(SpecialMethodInterface smi) {
143    this.smi = smi;
144  }
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
155class InfiniteWhileLoopWithIntrinsic extends SimpleLoopThread {
156  private String[] strings = { "a", "b", "c", "d" };
157  private int sum = 0;
158  public void run() {
159    int i = 0;
160    while (keepGoing) {
161      i++;
162      sum += strings[i & 3].length();
163    }
164  }
165}
166
167class InfiniteDoWhileLoopWithLong extends Thread {
168  volatile private long keepGoing = 7L;
169  public void run() {
170    int i = 0;
171    do {
172      i++;
173    } while (keepGoing >= 4L);
174  }
175  public void stopNow() {
176    keepGoing = 1L;
177  }
178}
179
180class InfiniteWhileLoop extends SimpleLoopThread {
181  public void run() {
182    int i = 0;
183    while (keepGoing) {
184      i++;
185    }
186  }
187}
188
189class InfiniteDoWhileLoop extends SimpleLoopThread {
190  public void run() {
191    int i = 0;
192    do {
193      i++;
194    } while (keepGoing);
195  }
196}
197
198class InfiniteForLoop extends SimpleLoopThread {
199  public void run() {
200    int i = 0;
201    for (int j = 0; keepGoing; j++) {
202      i += j;
203    }
204  }
205}
206
207class MakeGarbage extends SimpleLoopThread {
208  public void run() {
209    while (keepGoing) {
210      byte[] garbage = new byte[100000];
211    }
212  }
213}
214