jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 1 | // Copyright 2006 The Android Open Source Project |
| 2 | |
Elliott Hughes | 3b3e118 | 2011-10-06 13:53:01 -0700 | [diff] [blame^] | 3 | import java.util.ArrayList; |
| 4 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 5 | /** |
| 6 | * Test some basic thread stuff. |
| 7 | */ |
| 8 | public class Main { |
Elliott Hughes | 3b3e118 | 2011-10-06 13:53:01 -0700 | [diff] [blame^] | 9 | public static void main(String[] args) throws Exception { |
Elliott Hughes | 7502e2a | 2011-10-02 13:24:37 -0700 | [diff] [blame] | 10 | System.out.println("Initializing System.out..."); |
| 11 | |
Elliott Hughes | 3b3e118 | 2011-10-06 13:53:01 -0700 | [diff] [blame^] | 12 | MyThread[] threads = new MyThread[512]; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 13 | for (int i = 0; i < 512; i++) { |
Elliott Hughes | 3b3e118 | 2011-10-06 13:53:01 -0700 | [diff] [blame^] | 14 | threads[i] = new MyThread(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 15 | } |
| 16 | |
Elliott Hughes | 3b3e118 | 2011-10-06 13:53:01 -0700 | [diff] [blame^] | 17 | 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 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 26 | 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 Hughes | 3b3e118 | 2011-10-06 13:53:01 -0700 | [diff] [blame^] | 51 | static int mCount = 0; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 52 | public void run() { |
Elliott Hughes | 3b3e118 | 2011-10-06 13:53:01 -0700 | [diff] [blame^] | 53 | synchronized (MyThread.class) { |
| 54 | ++mCount; |
| 55 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | class 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 | } |