Home | History | Annotate | Download | only in src
      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();
     24         InfiniteDoWhileLoop doWhileLoop = new InfiniteDoWhileLoop();
     25         MakeGarbage garbage = new MakeGarbage();
     26         forLoop.start();
     27         whileLoop.start();
     28         doWhileLoop.start();
     29         garbage.start();
     30         for (int i = 0; i < TEST_TIME; i++) {
     31           System.gc();
     32           System.out.println(".");
     33           sleep(1000);
     34         }
     35         forLoop.stopNow();
     36         whileLoop.stopNow();
     37         doWhileLoop.stopNow();
     38         garbage.stopNow();
     39         System.out.println("Done.");
     40     }
     41 
     42     public static void sleep(int ms) {
     43         try {
     44             Thread.sleep(ms);
     45         } catch (InterruptedException ie) {
     46             System.err.println("sleep was interrupted");
     47         }
     48     }
     49 }
     50 
     51 class InfiniteWhileLoop extends Thread {
     52   volatile private boolean keepGoing = true;
     53   public void run() {
     54     int i = 0;
     55     while (keepGoing) {
     56       i++;
     57     }
     58   }
     59   public void stopNow() {
     60     keepGoing = false;
     61   }
     62 }
     63 
     64 class InfiniteDoWhileLoop extends Thread {
     65   volatile private boolean keepGoing = true;
     66   public void run() {
     67     int i = 0;
     68     do {
     69       i++;
     70     } while (keepGoing);
     71   }
     72   public void stopNow() {
     73     keepGoing = false;
     74   }
     75 }
     76 
     77 class InfiniteForLoop extends Thread {
     78   int count = 100000;
     79   volatile private boolean keepGoing = true;
     80   public void run() {
     81     int i = 0;
     82     for (int j = 0; keepGoing; j++) {
     83       i += j;
     84     }
     85   }
     86   public void stopNow() {
     87     keepGoing = false;
     88   }
     89 }
     90 
     91 
     92 class MakeGarbage extends Thread {
     93   volatile private boolean keepGoing = true;
     94   public void run() {
     95     while (keepGoing) {
     96       byte[] garbage = new byte[100000];
     97     }
     98   }
     99   public void stopNow() {
    100     keepGoing = false;
    101   }
    102 }
    103