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) ..."); |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 22 | InfiniteDoWhileLoopWithLong doWhileLoopWithLong = new InfiniteDoWhileLoopWithLong(); |
Vladimir Marko | 6ce3eba | 2015-02-16 13:05:59 +0000 | [diff] [blame] | 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 | }; |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 36 | doWhileLoopWithLong.start(); |
Vladimir Marko | 6ce3eba | 2015-02-16 13:05:59 +0000 | [diff] [blame] | 37 | for (SimpleLoopThread loop : simpleLoops) { |
| 38 | loop.start(); |
| 39 | } |
buzbee | cbcfaf3 | 2013-08-19 07:37:40 -0700 | [diff] [blame] | 40 | for (int i = 0; i < TEST_TIME; i++) { |
Mathieu Chartier | 7befd0e | 2014-02-03 17:48:41 -0800 | [diff] [blame] | 41 | Runtime.getRuntime().gc(); |
buzbee | cbcfaf3 | 2013-08-19 07:37:40 -0700 | [diff] [blame] | 42 | System.out.println("."); |
| 43 | sleep(1000); |
| 44 | } |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 45 | doWhileLoopWithLong.stopNow(); |
Vladimir Marko | 6ce3eba | 2015-02-16 13:05:59 +0000 | [diff] [blame] | 46 | for (SimpleLoopThread loop : simpleLoops) { |
| 47 | loop.stopNow(); |
| 48 | } |
buzbee | cbcfaf3 | 2013-08-19 07:37:40 -0700 | [diff] [blame] | 49 | 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 Marko | 6ce3eba | 2015-02-16 13:05:59 +0000 | [diff] [blame] | 61 | class SimpleLoopThread extends Thread { |
| 62 | volatile protected boolean keepGoing = true; |
| 63 | public void stopNow() { |
| 64 | keepGoing = false; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | interface SpecialMethodInterface { |
| 69 | long ReturnArgOrConst(long arg); |
| 70 | void PutOrNop(long arg); |
| 71 | long ConstOrIGet(); |
| 72 | } |
| 73 | |
| 74 | class 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 | |
| 85 | class 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 | |
| 98 | class 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 | |
| 111 | class 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 | |
| 125 | class 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 | |
| 138 | class InfiniteWhileLoopWithIntrinsic extends SimpleLoopThread { |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 139 | 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 Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | class 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 Marko | 6ce3eba | 2015-02-16 13:05:59 +0000 | [diff] [blame] | 163 | class InfiniteWhileLoop extends SimpleLoopThread { |
buzbee | cbcfaf3 | 2013-08-19 07:37:40 -0700 | [diff] [blame] | 164 | public void run() { |
| 165 | int i = 0; |
| 166 | while (keepGoing) { |
| 167 | i++; |
| 168 | } |
| 169 | } |
buzbee | cbcfaf3 | 2013-08-19 07:37:40 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Vladimir Marko | 6ce3eba | 2015-02-16 13:05:59 +0000 | [diff] [blame] | 172 | class InfiniteDoWhileLoop extends SimpleLoopThread { |
buzbee | cbcfaf3 | 2013-08-19 07:37:40 -0700 | [diff] [blame] | 173 | public void run() { |
| 174 | int i = 0; |
| 175 | do { |
| 176 | i++; |
| 177 | } while (keepGoing); |
| 178 | } |
buzbee | cbcfaf3 | 2013-08-19 07:37:40 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Vladimir Marko | 6ce3eba | 2015-02-16 13:05:59 +0000 | [diff] [blame] | 181 | class InfiniteForLoop extends SimpleLoopThread { |
buzbee | cbcfaf3 | 2013-08-19 07:37:40 -0700 | [diff] [blame] | 182 | public void run() { |
| 183 | int i = 0; |
| 184 | for (int j = 0; keepGoing; j++) { |
| 185 | i += j; |
| 186 | } |
| 187 | } |
buzbee | cbcfaf3 | 2013-08-19 07:37:40 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Vladimir Marko | 6ce3eba | 2015-02-16 13:05:59 +0000 | [diff] [blame] | 190 | class MakeGarbage extends SimpleLoopThread { |
buzbee | cbcfaf3 | 2013-08-19 07:37:40 -0700 | [diff] [blame] | 191 | public void run() { |
| 192 | while (keepGoing) { |
| 193 | byte[] garbage = new byte[100000]; |
| 194 | } |
| 195 | } |
buzbee | cbcfaf3 | 2013-08-19 07:37:40 -0700 | [diff] [blame] | 196 | } |