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