blob: 5364e2aaaad818a371670f5ff16e5da22eb9a51f [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
jeffhao5d1ac922011-09-29 17:41:15 -070016
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 */
24public 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);
Elliott Hughes741b5b72012-01-31 19:18:51 -080040 } catch (InterruptedException ie) {
jeffhao5d1ac922011-09-29 17:41:15 -070041 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
Alex Light8d9044b2014-07-22 14:04:31 -070053 synchronized (one) {
54 one.start();
55 try {
56 one.wait();
57 } catch (InterruptedException ie) {
58 System.out.println("INTERRUPT!");
59 ie.printStackTrace();
60 }
jeffhao5d1ac922011-09-29 17:41:15 -070061 }
62
Elliott Hughes741b5b72012-01-31 19:18:51 -080063 two.start();
64
jeffhao5d1ac922011-09-29 17:41:15 -070065 //System.out.println("main: off and running");
66
67 try {
68 one.join();
69 two.join();
Elliott Hughes741b5b72012-01-31 19:18:51 -080070 } catch (InterruptedException ie) {
jeffhao5d1ac922011-09-29 17:41:15 -070071 System.out.println("INTERRUPT!");
72 ie.printStackTrace();
73 }
74 System.out.println("main: all done");
75 }
76
77 static void interruptTest() {
78 SleepyThread sleepy, pesky;
79
80 sleepy = new SleepyThread(null);
81 pesky = new SleepyThread(sleepy);
82
83 sleepy.setPriority(4);
84 sleepy.start();
85 pesky.start();
86 pesky.setPriority(3);
87 }
88}
89
90class CpuThread extends Thread {
91 static Object mSyncable = new Object();
92 static int mCount = 0;
93 int mNumber;
94
95 CpuThread(int num) {
96 super("CpuThread " + num);
97 mNumber = num;
98 }
99
100 public void run() {
101 //System.out.print("thread running -- ");
102 //System.out.println(Thread.currentThread().getName());
103
Elliott Hughes741b5b72012-01-31 19:18:51 -0800104 synchronized (mSyncable) {
Alex Light8d9044b2014-07-22 14:04:31 -0700105 synchronized (this) {
106 this.notify();
107 }
Elliott Hughes741b5b72012-01-31 19:18:51 -0800108 for (int i = 0; i < 10; i++) {
109 output(mNumber);
110 }
jeffhao5d1ac922011-09-29 17:41:15 -0700111
Elliott Hughes741b5b72012-01-31 19:18:51 -0800112 System.out.print("Final result: ");
113 System.out.println(mCount);
114 }
jeffhao5d1ac922011-09-29 17:41:15 -0700115 }
116
117 void output(int num) {
Elliott Hughes741b5b72012-01-31 19:18:51 -0800118 int count = mCount;
jeffhao5d1ac922011-09-29 17:41:15 -0700119
Elliott Hughes741b5b72012-01-31 19:18:51 -0800120 System.out.print("going: ");
121 System.out.println(num);
jeffhao5d1ac922011-09-29 17:41:15 -0700122
Elliott Hughes741b5b72012-01-31 19:18:51 -0800123 /* burn CPU; adjust end value so we exceed scheduler quantum */
124 for (int j = 0; j < 5000; j++) {
125 ;
jeffhao5d1ac922011-09-29 17:41:15 -0700126 }
Elliott Hughes741b5b72012-01-31 19:18:51 -0800127
128 count++;
129 mCount = count;
jeffhao5d1ac922011-09-29 17:41:15 -0700130 }
131}
132
133class SleepyThread extends Thread {
134 private SleepyThread mOther;
135 private Integer[] mWaitOnMe; // any type of object will do
136
137 private static int count = 0;
138
139 SleepyThread(SleepyThread other) {
140 mOther = other;
141 mWaitOnMe = new Integer[] { 1, 2 };
142
143 setName("thread#" + count);
144 count++;
145 }
146
147 public void run() {
148 System.out.println("SleepyThread.run starting");
149
150 if (false) {
151 ThreadDeathHandler threadHandler =
152 new ThreadDeathHandler("SYNC THREAD");
153 Thread.currentThread().setUncaughtExceptionHandler(threadHandler);
154 throw new NullPointerException("die");
155 }
156
157 if (mOther == null) {
158 boolean intr = false;
159
160 try {
161 synchronized (mWaitOnMe) {
162 mWaitOnMe.wait(9000);
163 }
Elliott Hughes741b5b72012-01-31 19:18:51 -0800164 } catch (InterruptedException ie) {
jeffhao5d1ac922011-09-29 17:41:15 -0700165 // Expecting this; interrupted should be false.
166 System.out.println(Thread.currentThread().getName() +
167 " interrupted, flag=" + Thread.interrupted());
168 intr = true;
Elliott Hughes741b5b72012-01-31 19:18:51 -0800169 } catch (Exception ex) {
jeffhao5d1ac922011-09-29 17:41:15 -0700170 ex.printStackTrace();
171 }
172
173 if (!intr)
174 System.out.println("NOT INTERRUPTED");
175 } else {
176 try {
177 Thread.sleep(2000);
Elliott Hughes741b5b72012-01-31 19:18:51 -0800178 } catch (InterruptedException ie) {
jeffhao5d1ac922011-09-29 17:41:15 -0700179 System.out.println("PESKY INTERRUPTED?");
180 }
181
182 System.out.println("interrupting other (isAlive="
183 + mOther.isAlive() + ")");
184 mOther.interrupt();
185 }
186 }
187}