buzbee | cbcfaf3 | 2013-08-19 07:37:40 -0700 | [diff] [blame] | 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 | |
| 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 Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame^] | 24 | InfiniteWhileLoopWithIntrinsic whileLoopWithIntrinsic = |
| 25 | new InfiniteWhileLoopWithIntrinsic(); |
| 26 | InfiniteDoWhileLoopWithLong doWhileLoopWithLong = new InfiniteDoWhileLoopWithLong(); |
buzbee | cbcfaf3 | 2013-08-19 07:37:40 -0700 | [diff] [blame] | 27 | InfiniteDoWhileLoop doWhileLoop = new InfiniteDoWhileLoop(); |
| 28 | MakeGarbage garbage = new MakeGarbage(); |
| 29 | forLoop.start(); |
| 30 | whileLoop.start(); |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame^] | 31 | whileLoopWithIntrinsic.start(); |
| 32 | doWhileLoopWithLong.start(); |
buzbee | cbcfaf3 | 2013-08-19 07:37:40 -0700 | [diff] [blame] | 33 | doWhileLoop.start(); |
| 34 | garbage.start(); |
| 35 | for (int i = 0; i < TEST_TIME; i++) { |
Mathieu Chartier | 7befd0e | 2014-02-03 17:48:41 -0800 | [diff] [blame] | 36 | Runtime.getRuntime().gc(); |
buzbee | cbcfaf3 | 2013-08-19 07:37:40 -0700 | [diff] [blame] | 37 | System.out.println("."); |
| 38 | sleep(1000); |
| 39 | } |
| 40 | forLoop.stopNow(); |
| 41 | whileLoop.stopNow(); |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame^] | 42 | whileLoopWithIntrinsic.stopNow(); |
| 43 | doWhileLoopWithLong.stopNow(); |
buzbee | cbcfaf3 | 2013-08-19 07:37:40 -0700 | [diff] [blame] | 44 | 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 Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame^] | 58 | class 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 | |
| 74 | class 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 | |
buzbee | cbcfaf3 | 2013-08-19 07:37:40 -0700 | [diff] [blame] | 87 | class 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 | |
| 100 | class 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 | |
| 113 | class 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 | |
| 128 | class 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 | } |