blob: 608b7e08781721ba0d7e89a8f63841e96f5b5208 [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();
jeffhao5d1ac922011-09-29 17:41:15 -070028 System.out.println("thread test done");
29 }
30
jeffhao5d1ac922011-09-29 17:41:15 -070031 /*
32 * Simple thread capacity test.
33 */
Brian Carlstromcbaf9872014-02-10 14:15:56 -080034 private static void testThreadCapacity() throws Exception {
35 TestCapacityThread[] threads = new TestCapacityThread[512];
36 for (int i = 0; i < 512; i++) {
37 threads[i] = new TestCapacityThread();
38 }
39
40 for (TestCapacityThread thread : threads) {
41 thread.start();
42 }
43 for (TestCapacityThread thread : threads) {
44 thread.join();
45 }
46
47 System.out.println("testThreadCapacity thread count: " + TestCapacityThread.mCount);
48 }
49
50 private static class TestCapacityThread extends Thread {
Elliott Hughes3b3e1182011-10-06 13:53:01 -070051 static int mCount = 0;
jeffhao5d1ac922011-09-29 17:41:15 -070052 public void run() {
Brian Carlstromcbaf9872014-02-10 14:15:56 -080053 synchronized (TestCapacityThread.class) {
Elliott Hughes3b3e1182011-10-06 13:53:01 -070054 ++mCount;
55 }
jeffhao94d6df42012-11-26 16:02:12 -080056 try {
57 sleep(1000);
58 } catch (Exception ex) {
59 }
jeffhao5d1ac922011-09-29 17:41:15 -070060 }
61 }
jeffhao5d1ac922011-09-29 17:41:15 -070062
Brian Carlstromcbaf9872014-02-10 14:15:56 -080063 private static void testThreadDaemons() {
64 Thread t = new Thread(null, new TestDaemonThread(), "TestDaemonThread", 7168);
65
66 t.setDaemon(false);
67
68 System.out.print("testThreadDaemons starting thread '" + t.getName() + "'\n");
69 t.start();
jeffhao5d1ac922011-09-29 17:41:15 -070070
71 try {
Brian Carlstromcbaf9872014-02-10 14:15:56 -080072 t.join();
73 } catch (InterruptedException ex) {
74 ex.printStackTrace();
jeffhao5d1ac922011-09-29 17:41:15 -070075 }
76
Brian Carlstromcbaf9872014-02-10 14:15:56 -080077 System.out.print("testThreadDaemons finished\n");
78 }
79
80 private static class TestDaemonThread implements Runnable {
81 public void run() {
82 System.out.print("testThreadDaemons @ Thread running\n");
83
84 try {
85 Thread.currentThread().setDaemon(true);
86 System.out.print("testThreadDaemons @ FAILED: setDaemon() succeeded\n");
87 } catch (IllegalThreadStateException itse) {
88 System.out.print("testThreadDaemons @ Got expected setDaemon exception\n");
89 }
90
91 try {
92 Thread.sleep(2000);
93 }
94 catch (InterruptedException ie) {
95 System.out.print("testThreadDaemons @ Interrupted!\n");
96 }
97 finally {
98 System.out.print("testThreadDaemons @ Thread bailing\n");
99 }
100 }
101 }
102
103 private static void testSleepZero() throws Exception {
104 Thread.currentThread().interrupt();
jeffhao5d1ac922011-09-29 17:41:15 -0700105 try {
Brian Carlstromcbaf9872014-02-10 14:15:56 -0800106 Thread.sleep(0);
107 throw new AssertionError("unreachable");
108 } catch (InterruptedException e) {
109 if (Thread.currentThread().isInterrupted()) {
110 throw new AssertionError("thread is interrupted");
111 }
jeffhao5d1ac922011-09-29 17:41:15 -0700112 }
Brian Carlstromcbaf9872014-02-10 14:15:56 -0800113 System.out.print("testSleepZero finished\n");
jeffhao5d1ac922011-09-29 17:41:15 -0700114 }
115}