blob: 2e26b222657c8fe3c0b36097f6b9d52912b7e0a2 [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
Elliott Hughes3b3e1182011-10-06 13:53:01 -070017import java.util.ArrayList;
18
jeffhao5d1ac922011-09-29 17:41:15 -070019/**
20 * Test some basic thread stuff.
21 */
22public class Main {
Elliott Hughes3b3e1182011-10-06 13:53:01 -070023 public static void main(String[] args) throws Exception {
Mathieu Chartier031768a2015-08-27 10:25:02 -070024 System.loadLibrary(args[0]);
Brian Carlstromcbaf9872014-02-10 14:15:56 -080025 System.out.println("thread test starting");
26 testThreadCapacity();
27 testThreadDaemons();
28 testSleepZero();
Brian Carlstromba32de42014-08-27 23:43:46 -070029 testSetName();
Narayan Kamatha0b34512014-10-07 12:51:26 +010030 testThreadPriorities();
jeffhao5d1ac922011-09-29 17:41:15 -070031 System.out.println("thread test done");
32 }
33
jeffhao5d1ac922011-09-29 17:41:15 -070034 /*
35 * Simple thread capacity test.
36 */
Brian Carlstromcbaf9872014-02-10 14:15:56 -080037 private static void testThreadCapacity() throws Exception {
38 TestCapacityThread[] threads = new TestCapacityThread[512];
39 for (int i = 0; i < 512; i++) {
40 threads[i] = new TestCapacityThread();
41 }
42
43 for (TestCapacityThread thread : threads) {
44 thread.start();
45 }
46 for (TestCapacityThread thread : threads) {
47 thread.join();
48 }
49
50 System.out.println("testThreadCapacity thread count: " + TestCapacityThread.mCount);
51 }
52
53 private static class TestCapacityThread extends Thread {
Elliott Hughes3b3e1182011-10-06 13:53:01 -070054 static int mCount = 0;
jeffhao5d1ac922011-09-29 17:41:15 -070055 public void run() {
Brian Carlstromcbaf9872014-02-10 14:15:56 -080056 synchronized (TestCapacityThread.class) {
Elliott Hughes3b3e1182011-10-06 13:53:01 -070057 ++mCount;
58 }
jeffhao94d6df42012-11-26 16:02:12 -080059 try {
60 sleep(1000);
61 } catch (Exception ex) {
62 }
jeffhao5d1ac922011-09-29 17:41:15 -070063 }
64 }
jeffhao5d1ac922011-09-29 17:41:15 -070065
Brian Carlstromcbaf9872014-02-10 14:15:56 -080066 private static void testThreadDaemons() {
67 Thread t = new Thread(null, new TestDaemonThread(), "TestDaemonThread", 7168);
68
69 t.setDaemon(false);
70
71 System.out.print("testThreadDaemons starting thread '" + t.getName() + "'\n");
72 t.start();
jeffhao5d1ac922011-09-29 17:41:15 -070073
74 try {
Brian Carlstromcbaf9872014-02-10 14:15:56 -080075 t.join();
76 } catch (InterruptedException ex) {
77 ex.printStackTrace();
jeffhao5d1ac922011-09-29 17:41:15 -070078 }
79
Brian Carlstromcbaf9872014-02-10 14:15:56 -080080 System.out.print("testThreadDaemons finished\n");
81 }
82
83 private static class TestDaemonThread implements Runnable {
84 public void run() {
85 System.out.print("testThreadDaemons @ Thread running\n");
86
87 try {
88 Thread.currentThread().setDaemon(true);
89 System.out.print("testThreadDaemons @ FAILED: setDaemon() succeeded\n");
90 } catch (IllegalThreadStateException itse) {
91 System.out.print("testThreadDaemons @ Got expected setDaemon exception\n");
92 }
93
94 try {
95 Thread.sleep(2000);
96 }
97 catch (InterruptedException ie) {
98 System.out.print("testThreadDaemons @ Interrupted!\n");
99 }
100 finally {
101 System.out.print("testThreadDaemons @ Thread bailing\n");
102 }
103 }
104 }
105
106 private static void testSleepZero() throws Exception {
107 Thread.currentThread().interrupt();
jeffhao5d1ac922011-09-29 17:41:15 -0700108 try {
Brian Carlstromcbaf9872014-02-10 14:15:56 -0800109 Thread.sleep(0);
110 throw new AssertionError("unreachable");
111 } catch (InterruptedException e) {
112 if (Thread.currentThread().isInterrupted()) {
113 throw new AssertionError("thread is interrupted");
114 }
jeffhao5d1ac922011-09-29 17:41:15 -0700115 }
Brian Carlstromcbaf9872014-02-10 14:15:56 -0800116 System.out.print("testSleepZero finished\n");
jeffhao5d1ac922011-09-29 17:41:15 -0700117 }
Brian Carlstromba32de42014-08-27 23:43:46 -0700118
119 private static void testSetName() throws Exception {
120 System.out.print("testSetName starting\n");
121 Thread thread = new Thread() {
122 @Override
123 public void run() {
124 System.out.print("testSetName running\n");
125 }
126 };
127 thread.start();
128 thread.setName("HelloWorld"); // b/17302037 hang if setName called after start
129 if (!thread.getName().equals("HelloWorld")) {
130 throw new AssertionError("Unexpected thread name: " + thread.getName());
131 }
132 thread.join();
133 if (!thread.getName().equals("HelloWorld")) {
134 throw new AssertionError("Unexpected thread name after join: " + thread.getName());
135 }
136 System.out.print("testSetName finished\n");
137 }
Narayan Kamatha0b34512014-10-07 12:51:26 +0100138
139 private static void testThreadPriorities() throws Exception {
140 System.out.print("testThreadPriorities starting\n");
141
142 PriorityStoringThread t1 = new PriorityStoringThread(false);
143 t1.setPriority(Thread.MAX_PRIORITY);
144 t1.start();
145 t1.join();
146 if (supportsThreadPriorities() && (t1.getNativePriority() != Thread.MAX_PRIORITY)) {
147 System.out.print("thread priority for t1 was " + t1.getNativePriority() +
148 " [expected Thread.MAX_PRIORITY]\n");
149 }
150
151 PriorityStoringThread t2 = new PriorityStoringThread(true);
152 t2.start();
153 t2.join();
154 if (supportsThreadPriorities() && (t2.getNativePriority() != Thread.MAX_PRIORITY)) {
155 System.out.print("thread priority for t2 was " + t2.getNativePriority() +
156 " [expected Thread.MAX_PRIORITY]\n");
157 }
158
159 System.out.print("testThreadPriorities finished\n");
160 }
161
162 private static native int getNativePriority();
163 private static native boolean supportsThreadPriorities();
164
165 static class PriorityStoringThread extends Thread {
166 private final boolean setPriority;
167 private volatile int nativePriority;
168
169 public PriorityStoringThread(boolean setPriority) {
170 this.setPriority = setPriority;
171 this.nativePriority = -1;
172 }
173
174 @Override
175 public void run() {
176 if (setPriority) {
177 setPriority(Thread.MAX_PRIORITY);
178 }
179
180 nativePriority = Main.getNativePriority();
181 }
182
183 public int getNativePriority() {
184 return nativePriority;
185 }
186 }
jeffhao5d1ac922011-09-29 17:41:15 -0700187}