blob: ea587af90ebaf7000ab32eeb7703707663268164 [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 {
Elliott Hughes7502e2a2011-10-02 13:24:37 -070024 System.out.println("Initializing System.out...");
25
Elliott Hughes3b3e1182011-10-06 13:53:01 -070026 MyThread[] threads = new MyThread[512];
jeffhao5d1ac922011-09-29 17:41:15 -070027 for (int i = 0; i < 512; i++) {
Elliott Hughes3b3e1182011-10-06 13:53:01 -070028 threads[i] = new MyThread();
jeffhao5d1ac922011-09-29 17:41:15 -070029 }
30
Elliott Hughes3b3e1182011-10-06 13:53:01 -070031 for (MyThread thread : threads) {
32 thread.start();
33 }
34 for (MyThread thread : threads) {
35 thread.join();
36 }
37
38 System.out.println("Thread count: " + MyThread.mCount);
39
jeffhao5d1ac922011-09-29 17:41:15 -070040 go();
41 System.out.println("thread test done");
42 }
43
44 public static void go() {
45 Thread t = new Thread(null, new ThreadTestSub(), "Thready", 7168);
46
47 t.setDaemon(false);
48
49 System.out.print("Starting thread '" + t.getName() + "'\n");
50 t.start();
51
52 try {
53 t.join();
54 } catch (InterruptedException ex) {
55 ex.printStackTrace();
56 }
57
58 System.out.print("Thread starter returning\n");
59 }
60
61 /*
62 * Simple thread capacity test.
63 */
64 static class MyThread extends Thread {
Elliott Hughes3b3e1182011-10-06 13:53:01 -070065 static int mCount = 0;
jeffhao5d1ac922011-09-29 17:41:15 -070066 public void run() {
Elliott Hughes3b3e1182011-10-06 13:53:01 -070067 synchronized (MyThread.class) {
68 ++mCount;
69 }
jeffhao5d1ac922011-09-29 17:41:15 -070070 }
71 }
72}
73
74class ThreadTestSub implements Runnable {
75 public void run() {
76 System.out.print("@ Thread running\n");
77
78 try {
79 Thread.currentThread().setDaemon(true);
80 System.out.print("@ FAILED: setDaemon() succeeded\n");
81 } catch (IllegalThreadStateException itse) {
82 System.out.print("@ Got expected setDaemon exception\n");
83 }
84
85 //if (true)
86 // throw new NullPointerException();
87 try {
88 Thread.sleep(2000);
89 }
90 catch (InterruptedException ie) {
91 System.out.print("@ Interrupted!\n");
92 }
93 finally {
94 System.out.print("@ Thread bailing\n");
95 }
96 }
97}