Main.java revision 37c16453a92bbf1a47f042000318a1b60381017d
1/*
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 */
16
17import java.util.ArrayList;
18
19/**
20 * Test some basic thread stuff.
21 */
22public class Main {
23    public static void main(String[] args) throws Exception {
24        System.out.println("thread test starting");
25        testThreadCapacity();
26        testThreadDaemons();
27        testSleepZero();
28        testSetName();
29        System.out.println("thread test done");
30    }
31
32    /*
33     * Simple thread capacity test.
34     */
35    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 {
52        static int mCount = 0;
53        public void run() {
54            synchronized (TestCapacityThread.class) {
55                ++mCount;
56            }
57            try {
58                sleep(1000);
59            } catch (Exception ex) {
60            }
61        }
62    }
63
64    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();
71
72        try {
73            t.join();
74        } catch (InterruptedException ex) {
75            ex.printStackTrace();
76        }
77
78        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();
106        try {
107            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            }
113        }
114        System.out.print("testSleepZero finished\n");
115    }
116
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    }
136}
137