xref: /aosp_15_r20/art/test/051-thread/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2006 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker import java.util.ArrayList;
18*795d594fSAndroid Build Coastguard Worker import java.util.Arrays;
19*795d594fSAndroid Build Coastguard Worker import java.util.Iterator;
20*795d594fSAndroid Build Coastguard Worker import java.util.List;
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker /**
23*795d594fSAndroid Build Coastguard Worker  * Test some basic thread stuff.
24*795d594fSAndroid Build Coastguard Worker  */
25*795d594fSAndroid Build Coastguard Worker public class Main {
main(String[] args)26*795d594fSAndroid Build Coastguard Worker     public static void main(String[] args) throws Exception {
27*795d594fSAndroid Build Coastguard Worker         System.loadLibrary(args[0]);
28*795d594fSAndroid Build Coastguard Worker         System.out.println("thread test starting");
29*795d594fSAndroid Build Coastguard Worker         testThreadCapacity();
30*795d594fSAndroid Build Coastguard Worker         testThreadDaemons();
31*795d594fSAndroid Build Coastguard Worker         testSleepZero();
32*795d594fSAndroid Build Coastguard Worker         testSetName();
33*795d594fSAndroid Build Coastguard Worker         testThreadPriorities();
34*795d594fSAndroid Build Coastguard Worker         testMainThreadGroup();
35*795d594fSAndroid Build Coastguard Worker         testMainThreadAllStackTraces();
36*795d594fSAndroid Build Coastguard Worker         System.out.println("thread test done");
37*795d594fSAndroid Build Coastguard Worker     }
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker     /*
40*795d594fSAndroid Build Coastguard Worker      * Simple thread capacity test.
41*795d594fSAndroid Build Coastguard Worker      */
testThreadCapacity()42*795d594fSAndroid Build Coastguard Worker     private static void testThreadCapacity() throws Exception {
43*795d594fSAndroid Build Coastguard Worker         TestCapacityThread[] threads = new TestCapacityThread[128];
44*795d594fSAndroid Build Coastguard Worker         for (int i = 0; i < threads.length; i++) {
45*795d594fSAndroid Build Coastguard Worker             threads[i] = new TestCapacityThread();
46*795d594fSAndroid Build Coastguard Worker         }
47*795d594fSAndroid Build Coastguard Worker 
48*795d594fSAndroid Build Coastguard Worker         for (TestCapacityThread thread : threads) {
49*795d594fSAndroid Build Coastguard Worker             thread.start();
50*795d594fSAndroid Build Coastguard Worker         }
51*795d594fSAndroid Build Coastguard Worker         for (TestCapacityThread thread : threads) {
52*795d594fSAndroid Build Coastguard Worker             thread.join();
53*795d594fSAndroid Build Coastguard Worker         }
54*795d594fSAndroid Build Coastguard Worker 
55*795d594fSAndroid Build Coastguard Worker         System.out.println("testThreadCapacity thread count: " + TestCapacityThread.mCount);
56*795d594fSAndroid Build Coastguard Worker     }
57*795d594fSAndroid Build Coastguard Worker 
58*795d594fSAndroid Build Coastguard Worker     private static class TestCapacityThread extends Thread {
59*795d594fSAndroid Build Coastguard Worker         static int mCount = 0;
run()60*795d594fSAndroid Build Coastguard Worker         public void run() {
61*795d594fSAndroid Build Coastguard Worker             synchronized (TestCapacityThread.class) {
62*795d594fSAndroid Build Coastguard Worker                 ++mCount;
63*795d594fSAndroid Build Coastguard Worker             }
64*795d594fSAndroid Build Coastguard Worker             try {
65*795d594fSAndroid Build Coastguard Worker                 sleep(1000);
66*795d594fSAndroid Build Coastguard Worker             } catch (Exception ex) {
67*795d594fSAndroid Build Coastguard Worker             }
68*795d594fSAndroid Build Coastguard Worker         }
69*795d594fSAndroid Build Coastguard Worker     }
70*795d594fSAndroid Build Coastguard Worker 
testThreadDaemons()71*795d594fSAndroid Build Coastguard Worker     private static void testThreadDaemons() {
72*795d594fSAndroid Build Coastguard Worker         Thread t = new Thread(null, new TestDaemonThread(), "TestDaemonThread", 7168);
73*795d594fSAndroid Build Coastguard Worker 
74*795d594fSAndroid Build Coastguard Worker         t.setDaemon(false);
75*795d594fSAndroid Build Coastguard Worker 
76*795d594fSAndroid Build Coastguard Worker         System.out.print("testThreadDaemons starting thread '" + t.getName() + "'\n");
77*795d594fSAndroid Build Coastguard Worker         t.start();
78*795d594fSAndroid Build Coastguard Worker 
79*795d594fSAndroid Build Coastguard Worker         try {
80*795d594fSAndroid Build Coastguard Worker             t.join();
81*795d594fSAndroid Build Coastguard Worker         } catch (InterruptedException ex) {
82*795d594fSAndroid Build Coastguard Worker             ex.printStackTrace(System.out);
83*795d594fSAndroid Build Coastguard Worker         }
84*795d594fSAndroid Build Coastguard Worker 
85*795d594fSAndroid Build Coastguard Worker         System.out.print("testThreadDaemons finished\n");
86*795d594fSAndroid Build Coastguard Worker     }
87*795d594fSAndroid Build Coastguard Worker 
88*795d594fSAndroid Build Coastguard Worker     private static class TestDaemonThread implements Runnable {
run()89*795d594fSAndroid Build Coastguard Worker         public void run() {
90*795d594fSAndroid Build Coastguard Worker             System.out.print("testThreadDaemons @ Thread running\n");
91*795d594fSAndroid Build Coastguard Worker 
92*795d594fSAndroid Build Coastguard Worker             try {
93*795d594fSAndroid Build Coastguard Worker                 Thread.currentThread().setDaemon(true);
94*795d594fSAndroid Build Coastguard Worker                 System.out.print("testThreadDaemons @ FAILED: setDaemon() succeeded\n");
95*795d594fSAndroid Build Coastguard Worker             } catch (IllegalThreadStateException itse) {
96*795d594fSAndroid Build Coastguard Worker                 System.out.print("testThreadDaemons @ Got expected setDaemon exception\n");
97*795d594fSAndroid Build Coastguard Worker             }
98*795d594fSAndroid Build Coastguard Worker 
99*795d594fSAndroid Build Coastguard Worker             try {
100*795d594fSAndroid Build Coastguard Worker                 Thread.sleep(2000);
101*795d594fSAndroid Build Coastguard Worker             }
102*795d594fSAndroid Build Coastguard Worker             catch (InterruptedException ie) {
103*795d594fSAndroid Build Coastguard Worker                 System.out.print("testThreadDaemons @ Interrupted!\n");
104*795d594fSAndroid Build Coastguard Worker             }
105*795d594fSAndroid Build Coastguard Worker             finally {
106*795d594fSAndroid Build Coastguard Worker                 System.out.print("testThreadDaemons @ Thread bailing\n");
107*795d594fSAndroid Build Coastguard Worker             }
108*795d594fSAndroid Build Coastguard Worker         }
109*795d594fSAndroid Build Coastguard Worker     }
110*795d594fSAndroid Build Coastguard Worker 
testSleepZero()111*795d594fSAndroid Build Coastguard Worker     private static void testSleepZero() throws Exception {
112*795d594fSAndroid Build Coastguard Worker         Thread.currentThread().interrupt();
113*795d594fSAndroid Build Coastguard Worker         try {
114*795d594fSAndroid Build Coastguard Worker             Thread.sleep(0);
115*795d594fSAndroid Build Coastguard Worker             throw new AssertionError("unreachable");
116*795d594fSAndroid Build Coastguard Worker         } catch (InterruptedException e) {
117*795d594fSAndroid Build Coastguard Worker             if (Thread.currentThread().isInterrupted()) {
118*795d594fSAndroid Build Coastguard Worker                 throw new AssertionError("thread is interrupted");
119*795d594fSAndroid Build Coastguard Worker             }
120*795d594fSAndroid Build Coastguard Worker         }
121*795d594fSAndroid Build Coastguard Worker         System.out.print("testSleepZero finished\n");
122*795d594fSAndroid Build Coastguard Worker     }
123*795d594fSAndroid Build Coastguard Worker 
testSetName()124*795d594fSAndroid Build Coastguard Worker     private static void testSetName() throws Exception {
125*795d594fSAndroid Build Coastguard Worker         System.out.print("testSetName starting\n");
126*795d594fSAndroid Build Coastguard Worker         Thread thread = new Thread() {
127*795d594fSAndroid Build Coastguard Worker             @Override
128*795d594fSAndroid Build Coastguard Worker             public void run() {
129*795d594fSAndroid Build Coastguard Worker                 System.out.print("testSetName running\n");
130*795d594fSAndroid Build Coastguard Worker             }
131*795d594fSAndroid Build Coastguard Worker         };
132*795d594fSAndroid Build Coastguard Worker         thread.start();
133*795d594fSAndroid Build Coastguard Worker         thread.setName("HelloWorld");  // b/17302037 hang if setName called after start
134*795d594fSAndroid Build Coastguard Worker         if (!thread.getName().equals("HelloWorld")) {
135*795d594fSAndroid Build Coastguard Worker             throw new AssertionError("Unexpected thread name: " + thread.getName());
136*795d594fSAndroid Build Coastguard Worker         }
137*795d594fSAndroid Build Coastguard Worker         thread.join();
138*795d594fSAndroid Build Coastguard Worker         if (!thread.getName().equals("HelloWorld")) {
139*795d594fSAndroid Build Coastguard Worker             throw new AssertionError("Unexpected thread name after join: " + thread.getName());
140*795d594fSAndroid Build Coastguard Worker         }
141*795d594fSAndroid Build Coastguard Worker         System.out.print("testSetName finished\n");
142*795d594fSAndroid Build Coastguard Worker     }
143*795d594fSAndroid Build Coastguard Worker 
testThreadPriorities()144*795d594fSAndroid Build Coastguard Worker     private static void testThreadPriorities() throws Exception {
145*795d594fSAndroid Build Coastguard Worker         System.out.print("testThreadPriorities starting\n");
146*795d594fSAndroid Build Coastguard Worker 
147*795d594fSAndroid Build Coastguard Worker         PriorityStoringThread t1 = new PriorityStoringThread(false);
148*795d594fSAndroid Build Coastguard Worker         t1.setPriority(Thread.MAX_PRIORITY);
149*795d594fSAndroid Build Coastguard Worker         t1.start();
150*795d594fSAndroid Build Coastguard Worker         t1.join();
151*795d594fSAndroid Build Coastguard Worker         if (supportsThreadPriorities() && (t1.getNativePriority() != Thread.MAX_PRIORITY)) {
152*795d594fSAndroid Build Coastguard Worker             System.out.print("thread priority for t1 was " + t1.getNativePriority() +
153*795d594fSAndroid Build Coastguard Worker                 " [expected Thread.MAX_PRIORITY]\n");
154*795d594fSAndroid Build Coastguard Worker         }
155*795d594fSAndroid Build Coastguard Worker 
156*795d594fSAndroid Build Coastguard Worker         PriorityStoringThread t2 = new PriorityStoringThread(true);
157*795d594fSAndroid Build Coastguard Worker         t2.start();
158*795d594fSAndroid Build Coastguard Worker         t2.join();
159*795d594fSAndroid Build Coastguard Worker         if (supportsThreadPriorities() && (t2.getNativePriority() != Thread.MAX_PRIORITY)) {
160*795d594fSAndroid Build Coastguard Worker             System.out.print("thread priority for t2 was " + t2.getNativePriority() +
161*795d594fSAndroid Build Coastguard Worker                 " [expected Thread.MAX_PRIORITY]\n");
162*795d594fSAndroid Build Coastguard Worker         }
163*795d594fSAndroid Build Coastguard Worker 
164*795d594fSAndroid Build Coastguard Worker         System.out.print("testThreadPriorities finished\n");
165*795d594fSAndroid Build Coastguard Worker     }
166*795d594fSAndroid Build Coastguard Worker 
testMainThreadGroup()167*795d594fSAndroid Build Coastguard Worker     private static void testMainThreadGroup() {
168*795d594fSAndroid Build Coastguard Worker       Thread threads[] = new Thread[10];
169*795d594fSAndroid Build Coastguard Worker       Thread current = Thread.currentThread();
170*795d594fSAndroid Build Coastguard Worker       current.getThreadGroup().enumerate(threads);
171*795d594fSAndroid Build Coastguard Worker 
172*795d594fSAndroid Build Coastguard Worker       for (Thread t : threads) {
173*795d594fSAndroid Build Coastguard Worker         if (t == current) {
174*795d594fSAndroid Build Coastguard Worker           System.out.println("Found current Thread in ThreadGroup");
175*795d594fSAndroid Build Coastguard Worker           return;
176*795d594fSAndroid Build Coastguard Worker         }
177*795d594fSAndroid Build Coastguard Worker       }
178*795d594fSAndroid Build Coastguard Worker       throw new RuntimeException("Did not find main thread: " + Arrays.toString(threads));
179*795d594fSAndroid Build Coastguard Worker     }
180*795d594fSAndroid Build Coastguard Worker 
testMainThreadAllStackTraces()181*795d594fSAndroid Build Coastguard Worker     private static void testMainThreadAllStackTraces() {
182*795d594fSAndroid Build Coastguard Worker       StackTraceElement[] trace = Thread.getAllStackTraces().get(Thread.currentThread());
183*795d594fSAndroid Build Coastguard Worker       if (trace == null) {
184*795d594fSAndroid Build Coastguard Worker         throw new RuntimeException("Did not find main thread: " + Thread.getAllStackTraces());
185*795d594fSAndroid Build Coastguard Worker       }
186*795d594fSAndroid Build Coastguard Worker       List<StackTraceElement> list = Arrays.asList(trace);
187*795d594fSAndroid Build Coastguard Worker       Iterator<StackTraceElement> it = list.iterator();
188*795d594fSAndroid Build Coastguard Worker       while (it.hasNext()) {
189*795d594fSAndroid Build Coastguard Worker         StackTraceElement ste = it.next();
190*795d594fSAndroid Build Coastguard Worker         if (ste.getClassName().equals("Main")) {
191*795d594fSAndroid Build Coastguard Worker           if (!ste.getMethodName().equals("testMainThreadAllStackTraces")) {
192*795d594fSAndroid Build Coastguard Worker             throw new RuntimeException(list.toString());
193*795d594fSAndroid Build Coastguard Worker           }
194*795d594fSAndroid Build Coastguard Worker 
195*795d594fSAndroid Build Coastguard Worker           StackTraceElement ste2 = it.next();
196*795d594fSAndroid Build Coastguard Worker           if (!ste2.getClassName().equals("Main")) {
197*795d594fSAndroid Build Coastguard Worker             throw new RuntimeException(list.toString());
198*795d594fSAndroid Build Coastguard Worker           }
199*795d594fSAndroid Build Coastguard Worker           if (!ste2.getMethodName().equals("main")) {
200*795d594fSAndroid Build Coastguard Worker             throw new RuntimeException(list.toString());
201*795d594fSAndroid Build Coastguard Worker           }
202*795d594fSAndroid Build Coastguard Worker 
203*795d594fSAndroid Build Coastguard Worker           System.out.println("Found expected stack in getAllStackTraces()");
204*795d594fSAndroid Build Coastguard Worker           return;
205*795d594fSAndroid Build Coastguard Worker         }
206*795d594fSAndroid Build Coastguard Worker       }
207*795d594fSAndroid Build Coastguard Worker       throw new RuntimeException(list.toString());
208*795d594fSAndroid Build Coastguard Worker     }
209*795d594fSAndroid Build Coastguard Worker 
getNativePriority()210*795d594fSAndroid Build Coastguard Worker     private static native int getNativePriority();
supportsThreadPriorities()211*795d594fSAndroid Build Coastguard Worker     private static native boolean supportsThreadPriorities();
212*795d594fSAndroid Build Coastguard Worker 
213*795d594fSAndroid Build Coastguard Worker     static class PriorityStoringThread extends Thread {
214*795d594fSAndroid Build Coastguard Worker         private final boolean setPriority;
215*795d594fSAndroid Build Coastguard Worker         private volatile int nativePriority;
216*795d594fSAndroid Build Coastguard Worker 
PriorityStoringThread(boolean setPriority)217*795d594fSAndroid Build Coastguard Worker         public PriorityStoringThread(boolean setPriority) {
218*795d594fSAndroid Build Coastguard Worker             this.setPriority = setPriority;
219*795d594fSAndroid Build Coastguard Worker             this.nativePriority = -1;
220*795d594fSAndroid Build Coastguard Worker         }
221*795d594fSAndroid Build Coastguard Worker 
222*795d594fSAndroid Build Coastguard Worker         @Override
run()223*795d594fSAndroid Build Coastguard Worker         public void run() {
224*795d594fSAndroid Build Coastguard Worker             if (setPriority) {
225*795d594fSAndroid Build Coastguard Worker                 setPriority(Thread.MAX_PRIORITY);
226*795d594fSAndroid Build Coastguard Worker             }
227*795d594fSAndroid Build Coastguard Worker 
228*795d594fSAndroid Build Coastguard Worker             nativePriority = Main.getNativePriority();
229*795d594fSAndroid Build Coastguard Worker         }
230*795d594fSAndroid Build Coastguard Worker 
getNativePriority()231*795d594fSAndroid Build Coastguard Worker         public int getNativePriority() {
232*795d594fSAndroid Build Coastguard Worker             return nativePriority;
233*795d594fSAndroid Build Coastguard Worker         }
234*795d594fSAndroid Build Coastguard Worker     }
235*795d594fSAndroid Build Coastguard Worker }
236