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