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