blob: 390685d0492a04b0530e880dbd39bb7e1785e30f [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 {
Brian Carlstromcbaf9872014-02-10 14:15:56 -080024 System.out.println("thread test starting");
25 testThreadCapacity();
26 testThreadDaemons();
27 testSleepZero();
Brian Carlstromba32de42014-08-27 23:43:46 -070028 testSetName();
jeffhao5d1ac922011-09-29 17:41:15 -070029 System.out.println("thread test done");
30 }
31
jeffhao5d1ac922011-09-29 17:41:15 -070032 /*
33 * Simple thread capacity test.
34 */
Brian Carlstromcbaf9872014-02-10 14:15:56 -080035 private static void testThreadCapacity() throws Exception {
36 TestCapacityThread[] threads = new TestCapacityThread[512];
37 for (int i = 0; i < 512; i++) {
38 threads[i] = new TestCapacityThread();
39 }
40
41 for (TestCapacityThread thread : threads) {
42 thread.start();
43 }
44 for (TestCapacityThread thread : threads) {
45 thread.join();
46 }
47
48 System.out.println("testThreadCapacity thread count: " + TestCapacityThread.mCount);
49 }
50
51 private static class TestCapacityThread extends Thread {
Elliott Hughes3b3e1182011-10-06 13:53:01 -070052 static int mCount = 0;
jeffhao5d1ac922011-09-29 17:41:15 -070053 public void run() {
Brian Carlstromcbaf9872014-02-10 14:15:56 -080054 synchronized (TestCapacityThread.class) {
Elliott Hughes3b3e1182011-10-06 13:53:01 -070055 ++mCount;
56 }
jeffhao94d6df42012-11-26 16:02:12 -080057 try {
58 sleep(1000);
59 } catch (Exception ex) {
60 }
jeffhao5d1ac922011-09-29 17:41:15 -070061 }
62 }
jeffhao5d1ac922011-09-29 17:41:15 -070063
Brian Carlstromcbaf9872014-02-10 14:15:56 -080064 private static void testThreadDaemons() {
65 Thread t = new Thread(null, new TestDaemonThread(), "TestDaemonThread", 7168);
66
67 t.setDaemon(false);
68
69 System.out.print("testThreadDaemons starting thread '" + t.getName() + "'\n");
70 t.start();
jeffhao5d1ac922011-09-29 17:41:15 -070071
72 try {
Brian Carlstromcbaf9872014-02-10 14:15:56 -080073 t.join();
74 } catch (InterruptedException ex) {
75 ex.printStackTrace();
jeffhao5d1ac922011-09-29 17:41:15 -070076 }
77
Brian Carlstromcbaf9872014-02-10 14:15:56 -080078 System.out.print("testThreadDaemons finished\n");
79 }
80
81 private static class TestDaemonThread implements Runnable {
82 public void run() {
83 System.out.print("testThreadDaemons @ Thread running\n");
84
85 try {
86 Thread.currentThread().setDaemon(true);
87 System.out.print("testThreadDaemons @ FAILED: setDaemon() succeeded\n");
88 } catch (IllegalThreadStateException itse) {
89 System.out.print("testThreadDaemons @ Got expected setDaemon exception\n");
90 }
91
92 try {
93 Thread.sleep(2000);
94 }
95 catch (InterruptedException ie) {
96 System.out.print("testThreadDaemons @ Interrupted!\n");
97 }
98 finally {
99 System.out.print("testThreadDaemons @ Thread bailing\n");
100 }
101 }
102 }
103
104 private static void testSleepZero() throws Exception {
105 Thread.currentThread().interrupt();
jeffhao5d1ac922011-09-29 17:41:15 -0700106 try {
Brian Carlstromcbaf9872014-02-10 14:15:56 -0800107 Thread.sleep(0);
108 throw new AssertionError("unreachable");
109 } catch (InterruptedException e) {
110 if (Thread.currentThread().isInterrupted()) {
111 throw new AssertionError("thread is interrupted");
112 }
jeffhao5d1ac922011-09-29 17:41:15 -0700113 }
Brian Carlstromcbaf9872014-02-10 14:15:56 -0800114 System.out.print("testSleepZero finished\n");
jeffhao5d1ac922011-09-29 17:41:15 -0700115 }
Brian Carlstromba32de42014-08-27 23:43:46 -0700116
117 private static void testSetName() throws Exception {
118 System.out.print("testSetName starting\n");
119 Thread thread = new Thread() {
120 @Override
121 public void run() {
122 System.out.print("testSetName running\n");
123 }
124 };
125 thread.start();
126 thread.setName("HelloWorld"); // b/17302037 hang if setName called after start
127 if (!thread.getName().equals("HelloWorld")) {
128 throw new AssertionError("Unexpected thread name: " + thread.getName());
129 }
130 thread.join();
131 if (!thread.getName().equals("HelloWorld")) {
132 throw new AssertionError("Unexpected thread name after join: " + thread.getName());
133 }
134 System.out.print("testSetName finished\n");
135 }
jeffhao5d1ac922011-09-29 17:41:15 -0700136}