blob: 95ec1a1f916a21f10f90a77d1ab3b019ea6a5d8d [file] [log] [blame]
jeffhao5d1ac922011-09-29 17:41:15 -07001// Copyright 2006 The Android Open Source Project
2
Elliott Hughes3b3e1182011-10-06 13:53:01 -07003import java.util.ArrayList;
4
jeffhao5d1ac922011-09-29 17:41:15 -07005/**
6 * Test some basic thread stuff.
7 */
8public class Main {
Elliott Hughes3b3e1182011-10-06 13:53:01 -07009 public static void main(String[] args) throws Exception {
Elliott Hughes7502e2a2011-10-02 13:24:37 -070010 System.out.println("Initializing System.out...");
11
Elliott Hughes3b3e1182011-10-06 13:53:01 -070012 MyThread[] threads = new MyThread[512];
jeffhao5d1ac922011-09-29 17:41:15 -070013 for (int i = 0; i < 512; i++) {
Elliott Hughes3b3e1182011-10-06 13:53:01 -070014 threads[i] = new MyThread();
jeffhao5d1ac922011-09-29 17:41:15 -070015 }
16
Elliott Hughes3b3e1182011-10-06 13:53:01 -070017 for (MyThread thread : threads) {
18 thread.start();
19 }
20 for (MyThread thread : threads) {
21 thread.join();
22 }
23
24 System.out.println("Thread count: " + MyThread.mCount);
25
jeffhao5d1ac922011-09-29 17:41:15 -070026 go();
27 System.out.println("thread test done");
28 }
29
30 public static void go() {
31 Thread t = new Thread(null, new ThreadTestSub(), "Thready", 7168);
32
33 t.setDaemon(false);
34
35 System.out.print("Starting thread '" + t.getName() + "'\n");
36 t.start();
37
38 try {
39 t.join();
40 } catch (InterruptedException ex) {
41 ex.printStackTrace();
42 }
43
44 System.out.print("Thread starter returning\n");
45 }
46
47 /*
48 * Simple thread capacity test.
49 */
50 static class MyThread extends Thread {
Elliott Hughes3b3e1182011-10-06 13:53:01 -070051 static int mCount = 0;
jeffhao5d1ac922011-09-29 17:41:15 -070052 public void run() {
Elliott Hughes3b3e1182011-10-06 13:53:01 -070053 synchronized (MyThread.class) {
54 ++mCount;
55 }
jeffhao5d1ac922011-09-29 17:41:15 -070056 }
57 }
58}
59
60class ThreadTestSub implements Runnable {
61 public void run() {
62 System.out.print("@ Thread running\n");
63
64 try {
65 Thread.currentThread().setDaemon(true);
66 System.out.print("@ FAILED: setDaemon() succeeded\n");
67 } catch (IllegalThreadStateException itse) {
68 System.out.print("@ Got expected setDaemon exception\n");
69 }
70
71 //if (true)
72 // throw new NullPointerException();
73 try {
74 Thread.sleep(2000);
75 }
76 catch (InterruptedException ie) {
77 System.out.print("@ Interrupted!\n");
78 }
79 finally {
80 System.out.print("@ Thread bailing\n");
81 }
82 }
83}