1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2006 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker /** 18*795d594fSAndroid Build Coastguard Worker * Test synchronization primitives. 19*795d594fSAndroid Build Coastguard Worker * 20*795d594fSAndroid Build Coastguard Worker * TODO: this should be re-written to be a little more rigorous and/or 21*795d594fSAndroid Build Coastguard Worker * useful. Also, the ThreadDeathHandler stuff should be exposed or 22*795d594fSAndroid Build Coastguard Worker * split out. 23*795d594fSAndroid Build Coastguard Worker */ 24*795d594fSAndroid Build Coastguard Worker public class Main { main(String[] args)25*795d594fSAndroid Build Coastguard Worker public static void main(String[] args) { 26*795d594fSAndroid Build Coastguard Worker System.out.println("Sleep Test"); 27*795d594fSAndroid Build Coastguard Worker sleepTest(); 28*795d594fSAndroid Build Coastguard Worker 29*795d594fSAndroid Build Coastguard Worker System.out.println("\nCount Test"); 30*795d594fSAndroid Build Coastguard Worker countTest(); 31*795d594fSAndroid Build Coastguard Worker 32*795d594fSAndroid Build Coastguard Worker System.out.println("\nInterrupt Test"); 33*795d594fSAndroid Build Coastguard Worker interruptTest(); 34*795d594fSAndroid Build Coastguard Worker } 35*795d594fSAndroid Build Coastguard Worker sleepTest()36*795d594fSAndroid Build Coastguard Worker static void sleepTest() { 37*795d594fSAndroid Build Coastguard Worker System.out.println("GOING"); 38*795d594fSAndroid Build Coastguard Worker try { 39*795d594fSAndroid Build Coastguard Worker Thread.sleep(1000); 40*795d594fSAndroid Build Coastguard Worker } catch (InterruptedException ie) { 41*795d594fSAndroid Build Coastguard Worker System.out.println("INTERRUPT!"); 42*795d594fSAndroid Build Coastguard Worker ie.printStackTrace(System.out); 43*795d594fSAndroid Build Coastguard Worker } 44*795d594fSAndroid Build Coastguard Worker System.out.println("GONE"); 45*795d594fSAndroid Build Coastguard Worker } 46*795d594fSAndroid Build Coastguard Worker countTest()47*795d594fSAndroid Build Coastguard Worker static void countTest() { 48*795d594fSAndroid Build Coastguard Worker CpuThread one, two; 49*795d594fSAndroid Build Coastguard Worker 50*795d594fSAndroid Build Coastguard Worker one = new CpuThread(1); 51*795d594fSAndroid Build Coastguard Worker two = new CpuThread(2); 52*795d594fSAndroid Build Coastguard Worker 53*795d594fSAndroid Build Coastguard Worker synchronized (one) { 54*795d594fSAndroid Build Coastguard Worker one.start(); 55*795d594fSAndroid Build Coastguard Worker try { 56*795d594fSAndroid Build Coastguard Worker one.wait(); 57*795d594fSAndroid Build Coastguard Worker } catch (InterruptedException ie) { 58*795d594fSAndroid Build Coastguard Worker System.out.println("INTERRUPT!"); 59*795d594fSAndroid Build Coastguard Worker ie.printStackTrace(System.out); 60*795d594fSAndroid Build Coastguard Worker } 61*795d594fSAndroid Build Coastguard Worker } 62*795d594fSAndroid Build Coastguard Worker 63*795d594fSAndroid Build Coastguard Worker two.start(); 64*795d594fSAndroid Build Coastguard Worker 65*795d594fSAndroid Build Coastguard Worker //System.out.println("main: off and running"); 66*795d594fSAndroid Build Coastguard Worker 67*795d594fSAndroid Build Coastguard Worker try { 68*795d594fSAndroid Build Coastguard Worker one.join(); 69*795d594fSAndroid Build Coastguard Worker two.join(); 70*795d594fSAndroid Build Coastguard Worker } catch (InterruptedException ie) { 71*795d594fSAndroid Build Coastguard Worker System.out.println("INTERRUPT!"); 72*795d594fSAndroid Build Coastguard Worker ie.printStackTrace(System.out); 73*795d594fSAndroid Build Coastguard Worker } 74*795d594fSAndroid Build Coastguard Worker System.out.println("main: all done"); 75*795d594fSAndroid Build Coastguard Worker } 76*795d594fSAndroid Build Coastguard Worker interruptTest()77*795d594fSAndroid Build Coastguard Worker static void interruptTest() { 78*795d594fSAndroid Build Coastguard Worker SleepyThread sleepy, pesky; 79*795d594fSAndroid Build Coastguard Worker 80*795d594fSAndroid Build Coastguard Worker sleepy = new SleepyThread(null); 81*795d594fSAndroid Build Coastguard Worker pesky = new SleepyThread(sleepy); 82*795d594fSAndroid Build Coastguard Worker 83*795d594fSAndroid Build Coastguard Worker sleepy.setPriority(4); 84*795d594fSAndroid Build Coastguard Worker sleepy.start(); 85*795d594fSAndroid Build Coastguard Worker pesky.start(); 86*795d594fSAndroid Build Coastguard Worker pesky.setPriority(3); 87*795d594fSAndroid Build Coastguard Worker } 88*795d594fSAndroid Build Coastguard Worker } 89*795d594fSAndroid Build Coastguard Worker 90*795d594fSAndroid Build Coastguard Worker class CpuThread extends Thread { 91*795d594fSAndroid Build Coastguard Worker static Object mSyncable = new Object(); 92*795d594fSAndroid Build Coastguard Worker static int mCount = 0; 93*795d594fSAndroid Build Coastguard Worker int mNumber; 94*795d594fSAndroid Build Coastguard Worker CpuThread(int num)95*795d594fSAndroid Build Coastguard Worker CpuThread(int num) { 96*795d594fSAndroid Build Coastguard Worker super("CpuThread " + num); 97*795d594fSAndroid Build Coastguard Worker mNumber = num; 98*795d594fSAndroid Build Coastguard Worker } 99*795d594fSAndroid Build Coastguard Worker run()100*795d594fSAndroid Build Coastguard Worker public void run() { 101*795d594fSAndroid Build Coastguard Worker //System.out.print("thread running -- "); 102*795d594fSAndroid Build Coastguard Worker //System.out.println(Thread.currentThread().getName()); 103*795d594fSAndroid Build Coastguard Worker 104*795d594fSAndroid Build Coastguard Worker synchronized (mSyncable) { 105*795d594fSAndroid Build Coastguard Worker synchronized (this) { 106*795d594fSAndroid Build Coastguard Worker this.notify(); 107*795d594fSAndroid Build Coastguard Worker } 108*795d594fSAndroid Build Coastguard Worker for (int i = 0; i < 10; i++) { 109*795d594fSAndroid Build Coastguard Worker output(mNumber); 110*795d594fSAndroid Build Coastguard Worker } 111*795d594fSAndroid Build Coastguard Worker 112*795d594fSAndroid Build Coastguard Worker System.out.print("Final result: "); 113*795d594fSAndroid Build Coastguard Worker System.out.println(mCount); 114*795d594fSAndroid Build Coastguard Worker } 115*795d594fSAndroid Build Coastguard Worker } 116*795d594fSAndroid Build Coastguard Worker output(int num)117*795d594fSAndroid Build Coastguard Worker void output(int num) { 118*795d594fSAndroid Build Coastguard Worker int count = mCount; 119*795d594fSAndroid Build Coastguard Worker 120*795d594fSAndroid Build Coastguard Worker System.out.print("going: "); 121*795d594fSAndroid Build Coastguard Worker System.out.println(num); 122*795d594fSAndroid Build Coastguard Worker 123*795d594fSAndroid Build Coastguard Worker /* burn CPU; adjust end value so we exceed scheduler quantum */ 124*795d594fSAndroid Build Coastguard Worker for (int j = 0; j < 5000; j++) { 125*795d594fSAndroid Build Coastguard Worker ; 126*795d594fSAndroid Build Coastguard Worker } 127*795d594fSAndroid Build Coastguard Worker 128*795d594fSAndroid Build Coastguard Worker count++; 129*795d594fSAndroid Build Coastguard Worker mCount = count; 130*795d594fSAndroid Build Coastguard Worker } 131*795d594fSAndroid Build Coastguard Worker } 132*795d594fSAndroid Build Coastguard Worker 133*795d594fSAndroid Build Coastguard Worker class SleepyThread extends Thread { 134*795d594fSAndroid Build Coastguard Worker private SleepyThread mOther; 135*795d594fSAndroid Build Coastguard Worker private Integer[] mWaitOnMe; // any type of object will do 136*795d594fSAndroid Build Coastguard Worker private volatile boolean otherDone; 137*795d594fSAndroid Build Coastguard Worker 138*795d594fSAndroid Build Coastguard Worker private static int count = 0; 139*795d594fSAndroid Build Coastguard Worker SleepyThread(SleepyThread other)140*795d594fSAndroid Build Coastguard Worker SleepyThread(SleepyThread other) { 141*795d594fSAndroid Build Coastguard Worker mOther = other; 142*795d594fSAndroid Build Coastguard Worker otherDone = false; 143*795d594fSAndroid Build Coastguard Worker mWaitOnMe = new Integer[] { 1, 2 }; 144*795d594fSAndroid Build Coastguard Worker 145*795d594fSAndroid Build Coastguard Worker setName("thread#" + count); 146*795d594fSAndroid Build Coastguard Worker count++; 147*795d594fSAndroid Build Coastguard Worker } 148*795d594fSAndroid Build Coastguard Worker run()149*795d594fSAndroid Build Coastguard Worker public void run() { 150*795d594fSAndroid Build Coastguard Worker System.out.println("SleepyThread.run starting"); 151*795d594fSAndroid Build Coastguard Worker 152*795d594fSAndroid Build Coastguard Worker if (false) { 153*795d594fSAndroid Build Coastguard Worker ThreadDeathHandler threadHandler = 154*795d594fSAndroid Build Coastguard Worker new ThreadDeathHandler("SYNC THREAD"); 155*795d594fSAndroid Build Coastguard Worker Thread.currentThread().setUncaughtExceptionHandler(threadHandler); 156*795d594fSAndroid Build Coastguard Worker throw new NullPointerException("die"); 157*795d594fSAndroid Build Coastguard Worker } 158*795d594fSAndroid Build Coastguard Worker 159*795d594fSAndroid Build Coastguard Worker if (mOther == null) { 160*795d594fSAndroid Build Coastguard Worker boolean intr = false; 161*795d594fSAndroid Build Coastguard Worker 162*795d594fSAndroid Build Coastguard Worker try { 163*795d594fSAndroid Build Coastguard Worker do { 164*795d594fSAndroid Build Coastguard Worker synchronized (mWaitOnMe) { 165*795d594fSAndroid Build Coastguard Worker mWaitOnMe.wait(9000); 166*795d594fSAndroid Build Coastguard Worker } 167*795d594fSAndroid Build Coastguard Worker } while (!otherDone); 168*795d594fSAndroid Build Coastguard Worker } catch (InterruptedException ie) { 169*795d594fSAndroid Build Coastguard Worker // Expecting this; interrupted should be false. 170*795d594fSAndroid Build Coastguard Worker System.out.println(Thread.currentThread().getName() + 171*795d594fSAndroid Build Coastguard Worker " interrupted, flag=" + Thread.interrupted()); 172*795d594fSAndroid Build Coastguard Worker intr = true; 173*795d594fSAndroid Build Coastguard Worker } catch (Exception ex) { 174*795d594fSAndroid Build Coastguard Worker ex.printStackTrace(System.out); 175*795d594fSAndroid Build Coastguard Worker } 176*795d594fSAndroid Build Coastguard Worker 177*795d594fSAndroid Build Coastguard Worker if (!intr) 178*795d594fSAndroid Build Coastguard Worker System.out.println("NOT INTERRUPTED"); 179*795d594fSAndroid Build Coastguard Worker } else { 180*795d594fSAndroid Build Coastguard Worker try { 181*795d594fSAndroid Build Coastguard Worker Thread.sleep(2000); 182*795d594fSAndroid Build Coastguard Worker } catch (InterruptedException ie) { 183*795d594fSAndroid Build Coastguard Worker System.out.println("PESKY INTERRUPTED?"); 184*795d594fSAndroid Build Coastguard Worker } 185*795d594fSAndroid Build Coastguard Worker 186*795d594fSAndroid Build Coastguard Worker System.out.println("interrupting other (isAlive=" 187*795d594fSAndroid Build Coastguard Worker + mOther.isAlive() + ")"); 188*795d594fSAndroid Build Coastguard Worker mOther.interrupt(); 189*795d594fSAndroid Build Coastguard Worker mOther.otherDone = true; 190*795d594fSAndroid Build Coastguard Worker } 191*795d594fSAndroid Build Coastguard Worker } 192*795d594fSAndroid Build Coastguard Worker } 193