blob: 911c739d92119e331d6026c33fb12036101be630 [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 }
jeffhao94d6df42012-11-26 16:02:12 -080070 try {
71 sleep(1000);
72 } catch (Exception ex) {
73 }
jeffhao5d1ac922011-09-29 17:41:15 -070074 }
75 }
76}
77
78class ThreadTestSub implements Runnable {
79 public void run() {
80 System.out.print("@ Thread running\n");
81
82 try {
83 Thread.currentThread().setDaemon(true);
84 System.out.print("@ FAILED: setDaemon() succeeded\n");
85 } catch (IllegalThreadStateException itse) {
86 System.out.print("@ Got expected setDaemon exception\n");
87 }
88
89 //if (true)
90 // throw new NullPointerException();
91 try {
92 Thread.sleep(2000);
93 }
94 catch (InterruptedException ie) {
95 System.out.print("@ Interrupted!\n");
96 }
97 finally {
98 System.out.print("@ Thread bailing\n");
99 }
100 }
101}