blob: b81273ea4eb95c32f86228141f0e542fb9cea21b [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 {
Narayan Kamatha0b34512014-10-07 12:51:26 +010023 static {
24 System.loadLibrary("arttest");
25 }
26
Elliott Hughes3b3e1182011-10-06 13:53:01 -070027 public static void main(String[] args) throws Exception {
Brian Carlstromcbaf9872014-02-10 14:15:56 -080028 System.out.println("thread test starting");
29 testThreadCapacity();
30 testThreadDaemons();
31 testSleepZero();
Brian Carlstromba32de42014-08-27 23:43:46 -070032 testSetName();
Narayan Kamatha0b34512014-10-07 12:51:26 +010033 testThreadPriorities();
jeffhao5d1ac922011-09-29 17:41:15 -070034 System.out.println("thread test done");
35 }
36
jeffhao5d1ac922011-09-29 17:41:15 -070037 /*
38 * Simple thread capacity test.
39 */
Brian Carlstromcbaf9872014-02-10 14:15:56 -080040 private static void testThreadCapacity() throws Exception {
41 TestCapacityThread[] threads = new TestCapacityThread[512];
42 for (int i = 0; i < 512; i++) {
43 threads[i] = new TestCapacityThread();
44 }
45
46 for (TestCapacityThread thread : threads) {
47 thread.start();
48 }
49 for (TestCapacityThread thread : threads) {
50 thread.join();
51 }
52
53 System.out.println("testThreadCapacity thread count: " + TestCapacityThread.mCount);
54 }
55
56 private static class TestCapacityThread extends Thread {
Elliott Hughes3b3e1182011-10-06 13:53:01 -070057 static int mCount = 0;
jeffhao5d1ac922011-09-29 17:41:15 -070058 public void run() {
Brian Carlstromcbaf9872014-02-10 14:15:56 -080059 synchronized (TestCapacityThread.class) {
Elliott Hughes3b3e1182011-10-06 13:53:01 -070060 ++mCount;
61 }
jeffhao94d6df42012-11-26 16:02:12 -080062 try {
63 sleep(1000);
64 } catch (Exception ex) {
65 }
jeffhao5d1ac922011-09-29 17:41:15 -070066 }
67 }
jeffhao5d1ac922011-09-29 17:41:15 -070068
Brian Carlstromcbaf9872014-02-10 14:15:56 -080069 private static void testThreadDaemons() {
70 Thread t = new Thread(null, new TestDaemonThread(), "TestDaemonThread", 7168);
71
72 t.setDaemon(false);
73
74 System.out.print("testThreadDaemons starting thread '" + t.getName() + "'\n");
75 t.start();
jeffhao5d1ac922011-09-29 17:41:15 -070076
77 try {
Brian Carlstromcbaf9872014-02-10 14:15:56 -080078 t.join();
79 } catch (InterruptedException ex) {
80 ex.printStackTrace();
jeffhao5d1ac922011-09-29 17:41:15 -070081 }
82
Brian Carlstromcbaf9872014-02-10 14:15:56 -080083 System.out.print("testThreadDaemons finished\n");
84 }
85
86 private static class TestDaemonThread implements Runnable {
87 public void run() {
88 System.out.print("testThreadDaemons @ Thread running\n");
89
90 try {
91 Thread.currentThread().setDaemon(true);
92 System.out.print("testThreadDaemons @ FAILED: setDaemon() succeeded\n");
93 } catch (IllegalThreadStateException itse) {
94 System.out.print("testThreadDaemons @ Got expected setDaemon exception\n");
95 }
96
97 try {
98 Thread.sleep(2000);
99 }
100 catch (InterruptedException ie) {
101 System.out.print("testThreadDaemons @ Interrupted!\n");
102 }
103 finally {
104 System.out.print("testThreadDaemons @ Thread bailing\n");
105 }
106 }
107 }
108
109 private static void testSleepZero() throws Exception {
110 Thread.currentThread().interrupt();
jeffhao5d1ac922011-09-29 17:41:15 -0700111 try {
Brian Carlstromcbaf9872014-02-10 14:15:56 -0800112 Thread.sleep(0);
113 throw new AssertionError("unreachable");
114 } catch (InterruptedException e) {
115 if (Thread.currentThread().isInterrupted()) {
116 throw new AssertionError("thread is interrupted");
117 }
jeffhao5d1ac922011-09-29 17:41:15 -0700118 }
Brian Carlstromcbaf9872014-02-10 14:15:56 -0800119 System.out.print("testSleepZero finished\n");
jeffhao5d1ac922011-09-29 17:41:15 -0700120 }
Brian Carlstromba32de42014-08-27 23:43:46 -0700121
122 private static void testSetName() throws Exception {
123 System.out.print("testSetName starting\n");
124 Thread thread = new Thread() {
125 @Override
126 public void run() {
127 System.out.print("testSetName running\n");
128 }
129 };
130 thread.start();
131 thread.setName("HelloWorld"); // b/17302037 hang if setName called after start
132 if (!thread.getName().equals("HelloWorld")) {
133 throw new AssertionError("Unexpected thread name: " + thread.getName());
134 }
135 thread.join();
136 if (!thread.getName().equals("HelloWorld")) {
137 throw new AssertionError("Unexpected thread name after join: " + thread.getName());
138 }
139 System.out.print("testSetName finished\n");
140 }
Narayan Kamatha0b34512014-10-07 12:51:26 +0100141
142 private static void testThreadPriorities() throws Exception {
143 System.out.print("testThreadPriorities starting\n");
144
145 PriorityStoringThread t1 = new PriorityStoringThread(false);
146 t1.setPriority(Thread.MAX_PRIORITY);
147 t1.start();
148 t1.join();
149 if (supportsThreadPriorities() && (t1.getNativePriority() != Thread.MAX_PRIORITY)) {
150 System.out.print("thread priority for t1 was " + t1.getNativePriority() +
151 " [expected Thread.MAX_PRIORITY]\n");
152 }
153
154 PriorityStoringThread t2 = new PriorityStoringThread(true);
155 t2.start();
156 t2.join();
157 if (supportsThreadPriorities() && (t2.getNativePriority() != Thread.MAX_PRIORITY)) {
158 System.out.print("thread priority for t2 was " + t2.getNativePriority() +
159 " [expected Thread.MAX_PRIORITY]\n");
160 }
161
162 System.out.print("testThreadPriorities finished\n");
163 }
164
165 private static native int getNativePriority();
166 private static native boolean supportsThreadPriorities();
167
168 static class PriorityStoringThread extends Thread {
169 private final boolean setPriority;
170 private volatile int nativePriority;
171
172 public PriorityStoringThread(boolean setPriority) {
173 this.setPriority = setPriority;
174 this.nativePriority = -1;
175 }
176
177 @Override
178 public void run() {
179 if (setPriority) {
180 setPriority(Thread.MAX_PRIORITY);
181 }
182
183 nativePriority = Main.getNativePriority();
184 }
185
186 public int getNativePriority() {
187 return nativePriority;
188 }
189 }
jeffhao5d1ac922011-09-29 17:41:15 -0700190}