blob: cd5130d12749422ff439b7b1f6586ce1a23e2d69 [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) ...");
22 InfiniteForLoop forLoop = new InfiniteForLoop();
23 InfiniteWhileLoop whileLoop = new InfiniteWhileLoop();
Vladimir Marko8b858e12014-11-27 14:52:37 +000024 InfiniteWhileLoopWithIntrinsic whileLoopWithIntrinsic =
25 new InfiniteWhileLoopWithIntrinsic();
26 InfiniteDoWhileLoopWithLong doWhileLoopWithLong = new InfiniteDoWhileLoopWithLong();
buzbeecbcfaf32013-08-19 07:37:40 -070027 InfiniteDoWhileLoop doWhileLoop = new InfiniteDoWhileLoop();
28 MakeGarbage garbage = new MakeGarbage();
29 forLoop.start();
30 whileLoop.start();
Vladimir Marko8b858e12014-11-27 14:52:37 +000031 whileLoopWithIntrinsic.start();
32 doWhileLoopWithLong.start();
buzbeecbcfaf32013-08-19 07:37:40 -070033 doWhileLoop.start();
34 garbage.start();
35 for (int i = 0; i < TEST_TIME; i++) {
Mathieu Chartier7befd0e2014-02-03 17:48:41 -080036 Runtime.getRuntime().gc();
buzbeecbcfaf32013-08-19 07:37:40 -070037 System.out.println(".");
38 sleep(1000);
39 }
40 forLoop.stopNow();
41 whileLoop.stopNow();
Vladimir Marko8b858e12014-11-27 14:52:37 +000042 whileLoopWithIntrinsic.stopNow();
43 doWhileLoopWithLong.stopNow();
buzbeecbcfaf32013-08-19 07:37:40 -070044 doWhileLoop.stopNow();
45 garbage.stopNow();
46 System.out.println("Done.");
47 }
48
49 public static void sleep(int ms) {
50 try {
51 Thread.sleep(ms);
52 } catch (InterruptedException ie) {
53 System.err.println("sleep was interrupted");
54 }
55 }
56}
57
Vladimir Marko8b858e12014-11-27 14:52:37 +000058class InfiniteWhileLoopWithIntrinsic extends Thread {
59 volatile private boolean keepGoing = true;
60 private String[] strings = { "a", "b", "c", "d" };
61 private int sum = 0;
62 public void run() {
63 int i = 0;
64 while (keepGoing) {
65 i++;
66 sum += strings[i & 3].length();
67 }
68 }
69 public void stopNow() {
70 keepGoing = false;
71 }
72}
73
74class InfiniteDoWhileLoopWithLong extends Thread {
75 volatile private long keepGoing = 7L;
76 public void run() {
77 int i = 0;
78 do {
79 i++;
80 } while (keepGoing >= 4L);
81 }
82 public void stopNow() {
83 keepGoing = 1L;
84 }
85}
86
buzbeecbcfaf32013-08-19 07:37:40 -070087class InfiniteWhileLoop extends Thread {
88 volatile private boolean keepGoing = true;
89 public void run() {
90 int i = 0;
91 while (keepGoing) {
92 i++;
93 }
94 }
95 public void stopNow() {
96 keepGoing = false;
97 }
98}
99
100class InfiniteDoWhileLoop extends Thread {
101 volatile private boolean keepGoing = true;
102 public void run() {
103 int i = 0;
104 do {
105 i++;
106 } while (keepGoing);
107 }
108 public void stopNow() {
109 keepGoing = false;
110 }
111}
112
113class InfiniteForLoop extends Thread {
114 int count = 100000;
115 volatile private boolean keepGoing = true;
116 public void run() {
117 int i = 0;
118 for (int j = 0; keepGoing; j++) {
119 i += j;
120 }
121 }
122 public void stopNow() {
123 keepGoing = false;
124 }
125}
126
127
128class MakeGarbage extends Thread {
129 volatile private boolean keepGoing = true;
130 public void run() {
131 while (keepGoing) {
132 byte[] garbage = new byte[100000];
133 }
134 }
135 public void stopNow() {
136 keepGoing = false;
137 }
138}