blob: ba378187d922a1a358b1289d72e15b4c833085e3 [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!");
Kevin Brodskyf6c66c32015-12-17 14:13:00 +000042 ie.printStackTrace(System.out);
jeffhao5d1ac922011-09-29 17:41:15 -070043 }
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!");
Kevin Brodskyf6c66c32015-12-17 14:13:00 +000059 ie.printStackTrace(System.out);
Alex Light8d9044b2014-07-22 14:04:31 -070060 }
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!");
Kevin Brodskyf6c66c32015-12-17 14:13:00 +000072 ie.printStackTrace(System.out);
jeffhao5d1ac922011-09-29 17:41:15 -070073 }
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
Lokesh Gidra6338c5f2018-10-12 00:44:38 -0700136 private volatile boolean otherDone;
jeffhao5d1ac922011-09-29 17:41:15 -0700137
138 private static int count = 0;
139
140 SleepyThread(SleepyThread other) {
141 mOther = other;
Lokesh Gidra6338c5f2018-10-12 00:44:38 -0700142 otherDone = false;
jeffhao5d1ac922011-09-29 17:41:15 -0700143 mWaitOnMe = new Integer[] { 1, 2 };
144
145 setName("thread#" + count);
146 count++;
147 }
148
149 public void run() {
150 System.out.println("SleepyThread.run starting");
151
152 if (false) {
153 ThreadDeathHandler threadHandler =
154 new ThreadDeathHandler("SYNC THREAD");
155 Thread.currentThread().setUncaughtExceptionHandler(threadHandler);
156 throw new NullPointerException("die");
157 }
158
159 if (mOther == null) {
160 boolean intr = false;
161
162 try {
Lokesh Gidra6338c5f2018-10-12 00:44:38 -0700163 do {
jeffhao5d1ac922011-09-29 17:41:15 -0700164 synchronized (mWaitOnMe) {
165 mWaitOnMe.wait(9000);
166 }
Lokesh Gidra6338c5f2018-10-12 00:44:38 -0700167 } while (!otherDone);
Elliott Hughes741b5b72012-01-31 19:18:51 -0800168 } catch (InterruptedException ie) {
jeffhao5d1ac922011-09-29 17:41:15 -0700169 // Expecting this; interrupted should be false.
170 System.out.println(Thread.currentThread().getName() +
171 " interrupted, flag=" + Thread.interrupted());
172 intr = true;
Elliott Hughes741b5b72012-01-31 19:18:51 -0800173 } catch (Exception ex) {
Kevin Brodskyf6c66c32015-12-17 14:13:00 +0000174 ex.printStackTrace(System.out);
jeffhao5d1ac922011-09-29 17:41:15 -0700175 }
176
177 if (!intr)
178 System.out.println("NOT INTERRUPTED");
179 } else {
180 try {
181 Thread.sleep(2000);
Elliott Hughes741b5b72012-01-31 19:18:51 -0800182 } catch (InterruptedException ie) {
jeffhao5d1ac922011-09-29 17:41:15 -0700183 System.out.println("PESKY INTERRUPTED?");
184 }
185
186 System.out.println("interrupting other (isAlive="
187 + mOther.isAlive() + ")");
188 mOther.interrupt();
Lokesh Gidra6338c5f2018-10-12 00:44:38 -0700189 mOther.otherDone = true;
jeffhao5d1ac922011-09-29 17:41:15 -0700190 }
191 }
192}