blob: 8046d751ed7bab83bb5bf84005c40db43b852c70 [file] [log] [blame]
buzbeecbcfaf32013-08-19 07:37:40 -07001/*
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) ...");
Vladimir Marko8b858e12014-11-27 14:52:37 +000022 InfiniteDoWhileLoopWithLong doWhileLoopWithLong = new InfiniteDoWhileLoopWithLong();
Vladimir Marko6ce3eba2015-02-16 13:05:59 +000023 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 };
Vladimir Marko8b858e12014-11-27 14:52:37 +000036 doWhileLoopWithLong.start();
Vladimir Marko6ce3eba2015-02-16 13:05:59 +000037 for (SimpleLoopThread loop : simpleLoops) {
38 loop.start();
39 }
buzbeecbcfaf32013-08-19 07:37:40 -070040 for (int i = 0; i < TEST_TIME; i++) {
Mathieu Chartier7befd0e2014-02-03 17:48:41 -080041 Runtime.getRuntime().gc();
buzbeecbcfaf32013-08-19 07:37:40 -070042 System.out.println(".");
43 sleep(1000);
44 }
Vladimir Marko8b858e12014-11-27 14:52:37 +000045 doWhileLoopWithLong.stopNow();
Vladimir Marko6ce3eba2015-02-16 13:05:59 +000046 for (SimpleLoopThread loop : simpleLoops) {
47 loop.stopNow();
48 }
buzbeecbcfaf32013-08-19 07:37:40 -070049 System.out.println("Done.");
50 }
51
52 public static void sleep(int ms) {
53 try {
54 Thread.sleep(ms);
55 } catch (InterruptedException ie) {
56 System.err.println("sleep was interrupted");
57 }
58 }
59}
60
Vladimir Marko6ce3eba2015-02-16 13:05:59 +000061class SimpleLoopThread extends Thread {
62 volatile protected boolean keepGoing = true;
63 public void stopNow() {
64 keepGoing = false;
65 }
66}
67
68interface SpecialMethodInterface {
69 long ReturnArgOrConst(long arg);
70 void PutOrNop(long arg);
71 long ConstOrIGet();
72}
73
74class SpecialMethods1 implements SpecialMethodInterface {
75 public long ReturnArgOrConst(long arg) {
76 return 42L;
77 }
78 public void PutOrNop(long arg) {
79 }
80 public long ConstOrIGet() {
81 return 42L;
82 }
83}
84
85class SpecialMethods2 implements SpecialMethodInterface {
86 public long value = 42L;
87 public long ReturnArgOrConst(long arg) {
88 return arg;
89 }
90 public void PutOrNop(long arg) {
91 value = arg;
92 }
93 public long ConstOrIGet() {
94 return value;
95 }
96}
97
98class InfiniteWhileLoopWithSpecialReturnArgOrConst extends SimpleLoopThread {
99 private SpecialMethodInterface smi;
100 public InfiniteWhileLoopWithSpecialReturnArgOrConst(SpecialMethodInterface smi) {
101 this.smi = smi;
102 }
103 public void run() {
104 long i = 0L;
105 while (keepGoing) {
106 i += smi.ReturnArgOrConst(i);
107 }
108 }
109}
110
111class InfiniteWhileLoopWithSpecialPutOrNop extends SimpleLoopThread {
112 private SpecialMethodInterface smi;
113 public InfiniteWhileLoopWithSpecialPutOrNop(SpecialMethodInterface smi) {
114 this.smi = smi;
115 }
116 public void run() {
117 long i = 0L;
118 while (keepGoing) {
119 smi.PutOrNop(i);
120 i++;
121 }
122 }
123}
124
125class InfiniteWhileLoopWithSpecialConstOrIGet extends SimpleLoopThread {
126 private SpecialMethodInterface smi;
127 public InfiniteWhileLoopWithSpecialConstOrIGet(SpecialMethodInterface smi) {
128 this.smi = smi;
129 }
130 public void run() {
131 long i = 0L;
132 while (keepGoing) {
133 i += smi.ConstOrIGet();
134 }
135 }
136}
137
138class InfiniteWhileLoopWithIntrinsic extends SimpleLoopThread {
Vladimir Marko8b858e12014-11-27 14:52:37 +0000139 private String[] strings = { "a", "b", "c", "d" };
140 private int sum = 0;
141 public void run() {
142 int i = 0;
143 while (keepGoing) {
144 i++;
145 sum += strings[i & 3].length();
146 }
147 }
Vladimir Marko8b858e12014-11-27 14:52:37 +0000148}
149
150class InfiniteDoWhileLoopWithLong extends Thread {
151 volatile private long keepGoing = 7L;
152 public void run() {
153 int i = 0;
154 do {
155 i++;
156 } while (keepGoing >= 4L);
157 }
158 public void stopNow() {
159 keepGoing = 1L;
160 }
161}
162
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000163class InfiniteWhileLoop extends SimpleLoopThread {
buzbeecbcfaf32013-08-19 07:37:40 -0700164 public void run() {
165 int i = 0;
166 while (keepGoing) {
167 i++;
168 }
169 }
buzbeecbcfaf32013-08-19 07:37:40 -0700170}
171
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000172class InfiniteDoWhileLoop extends SimpleLoopThread {
buzbeecbcfaf32013-08-19 07:37:40 -0700173 public void run() {
174 int i = 0;
175 do {
176 i++;
177 } while (keepGoing);
178 }
buzbeecbcfaf32013-08-19 07:37:40 -0700179}
180
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000181class InfiniteForLoop extends SimpleLoopThread {
buzbeecbcfaf32013-08-19 07:37:40 -0700182 public void run() {
183 int i = 0;
184 for (int j = 0; keepGoing; j++) {
185 i += j;
186 }
187 }
buzbeecbcfaf32013-08-19 07:37:40 -0700188}
189
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000190class MakeGarbage extends SimpleLoopThread {
buzbeecbcfaf32013-08-19 07:37:40 -0700191 public void run() {
192 while (keepGoing) {
193 byte[] garbage = new byte[100000];
194 }
195 }
buzbeecbcfaf32013-08-19 07:37:40 -0700196}