1 /* 2 * Copyright (C) 2006 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 /** 18 * Test synchronization primitives. 19 * 20 * TODO: this should be re-written to be a little more rigorous and/or 21 * useful. Also, the ThreadDeathHandler stuff should be exposed or 22 * split out. 23 */ 24 public class Main { 25 public static void main(String[] args) { 26 System.out.println("Sleep Test"); 27 sleepTest(); 28 29 System.out.println("\nCount Test"); 30 countTest(); 31 32 System.out.println("\nInterrupt Test"); 33 interruptTest(); 34 } 35 36 static void sleepTest() { 37 System.out.println("GOING"); 38 try { 39 Thread.sleep(1000); 40 } catch (InterruptedException ie) { 41 System.out.println("INTERRUPT!"); 42 ie.printStackTrace(); 43 } 44 System.out.println("GONE"); 45 } 46 47 static void countTest() { 48 CpuThread one, two; 49 50 one = new CpuThread(1); 51 two = new CpuThread(2); 52 53 one.start(); 54 55 try { 56 Thread.sleep(100); 57 } catch (InterruptedException ie) { 58 System.out.println("INTERRUPT!"); 59 ie.printStackTrace(); 60 } 61 62 two.start(); 63 64 //System.out.println("main: off and running"); 65 66 try { 67 one.join(); 68 two.join(); 69 } catch (InterruptedException ie) { 70 System.out.println("INTERRUPT!"); 71 ie.printStackTrace(); 72 } 73 System.out.println("main: all done"); 74 } 75 76 static void interruptTest() { 77 SleepyThread sleepy, pesky; 78 79 sleepy = new SleepyThread(null); 80 pesky = new SleepyThread(sleepy); 81 82 sleepy.setPriority(4); 83 sleepy.start(); 84 pesky.start(); 85 pesky.setPriority(3); 86 } 87 } 88 89 class CpuThread extends Thread { 90 static Object mSyncable = new Object(); 91 static int mCount = 0; 92 int mNumber; 93 94 CpuThread(int num) { 95 super("CpuThread " + num); 96 mNumber = num; 97 } 98 99 public void run() { 100 //System.out.print("thread running -- "); 101 //System.out.println(Thread.currentThread().getName()); 102 103 synchronized (mSyncable) { 104 for (int i = 0; i < 10; i++) { 105 output(mNumber); 106 } 107 108 System.out.print("Final result: "); 109 System.out.println(mCount); 110 } 111 } 112 113 void output(int num) { 114 int count = mCount; 115 116 System.out.print("going: "); 117 System.out.println(num); 118 119 /* burn CPU; adjust end value so we exceed scheduler quantum */ 120 for (int j = 0; j < 5000; j++) { 121 ; 122 } 123 124 count++; 125 mCount = count; 126 } 127 } 128 129 class SleepyThread extends Thread { 130 private SleepyThread mOther; 131 private Integer[] mWaitOnMe; // any type of object will do 132 133 private static int count = 0; 134 135 SleepyThread(SleepyThread other) { 136 mOther = other; 137 mWaitOnMe = new Integer[] { 1, 2 }; 138 139 setName("thread#" + count); 140 count++; 141 } 142 143 public void run() { 144 System.out.println("SleepyThread.run starting"); 145 146 if (false) { 147 ThreadDeathHandler threadHandler = 148 new ThreadDeathHandler("SYNC THREAD"); 149 Thread.currentThread().setUncaughtExceptionHandler(threadHandler); 150 throw new NullPointerException("die"); 151 } 152 153 if (mOther == null) { 154 boolean intr = false; 155 156 try { 157 synchronized (mWaitOnMe) { 158 mWaitOnMe.wait(9000); 159 } 160 } catch (InterruptedException ie) { 161 // Expecting this; interrupted should be false. 162 System.out.println(Thread.currentThread().getName() + 163 " interrupted, flag=" + Thread.interrupted()); 164 intr = true; 165 } catch (Exception ex) { 166 ex.printStackTrace(); 167 } 168 169 if (!intr) 170 System.out.println("NOT INTERRUPTED"); 171 } else { 172 try { 173 Thread.sleep(2000); 174 } catch (InterruptedException ie) { 175 System.out.println("PESKY INTERRUPTED?"); 176 } 177 178 System.out.println("interrupting other (isAlive=" 179 + mOther.isAlive() + ")"); 180 mOther.interrupt(); 181 } 182 } 183 } 184