Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | */ |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 16 | |
| 17 | /** |
| 18 | * Exercise Object.wait(), comparing results against wall clock time. |
| 19 | */ |
| 20 | public class Main { |
| 21 | /* delays, in milliseconds */ |
| 22 | private final static long[] DELAYS = { |
| 23 | 200, 500, 1000, 2000, 3500, 8000 |
| 24 | }; |
| 25 | |
| 26 | public static void main(String[] args) { |
| 27 | boolean timing = (args.length >= 1) && args[0].equals("--timing"); |
| 28 | doit(timing); |
| 29 | } |
| 30 | |
| 31 | public static void doit(boolean timing) { |
| 32 | Object sleepy = new Object(); |
| 33 | long start, end; |
| 34 | |
| 35 | synchronized (sleepy) { |
| 36 | try { |
| 37 | sleepy.wait(-500); |
| 38 | System.out.println("HEY: didn't throw on negative arg"); |
| 39 | } catch (IllegalArgumentException iae) { |
| 40 | System.out.println("Caught expected exception on neg arg"); |
| 41 | } catch (InterruptedException ie) { |
| 42 | ie.printStackTrace(); |
| 43 | } |
| 44 | |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 45 | for (long delay : DELAYS) { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 46 | System.out.println("Waiting for " + delay + "ms..."); |
| 47 | |
| 48 | start = System.currentTimeMillis(); |
| 49 | try { |
| 50 | sleepy.wait(delay); |
| 51 | } catch (InterruptedException ie) { |
| 52 | ie.printStackTrace(); |
| 53 | } |
| 54 | end = System.currentTimeMillis(); |
| 55 | |
| 56 | long elapsed = end - start; |
| 57 | boolean showTime = timing; |
| 58 | |
| 59 | if (! timing) { |
| 60 | long epsilon = delay / 10; |
| 61 | if (epsilon > 50) { |
| 62 | epsilon = 50; |
| 63 | } |
| 64 | |
| 65 | long min = delay - epsilon; |
| 66 | long max = delay + epsilon; |
| 67 | |
| 68 | if (elapsed < min) { |
| 69 | System.out.println(" Elapsed time was too short"); |
| 70 | showTime = true; |
| 71 | } else if (elapsed > max) { |
| 72 | System.out.println(" Elapsed time was too long: " |
| 73 | + "elapsed=" + elapsed + " max=" + max); |
| 74 | showTime = true; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | if (showTime) { |
| 79 | System.out.println(" Wall clock elapsed " |
| 80 | + elapsed + "ms"); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |