Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 16 | |
Elliott Hughes | 3b3e118 | 2011-10-06 13:53:01 -0700 | [diff] [blame] | 17 | import java.util.ArrayList; |
| 18 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 19 | /** |
| 20 | * Test some basic thread stuff. |
| 21 | */ |
| 22 | public class Main { |
Elliott Hughes | 3b3e118 | 2011-10-06 13:53:01 -0700 | [diff] [blame] | 23 | public static void main(String[] args) throws Exception { |
Elliott Hughes | 7502e2a | 2011-10-02 13:24:37 -0700 | [diff] [blame] | 24 | System.out.println("Initializing System.out..."); |
| 25 | |
Elliott Hughes | 3b3e118 | 2011-10-06 13:53:01 -0700 | [diff] [blame] | 26 | MyThread[] threads = new MyThread[512]; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 27 | for (int i = 0; i < 512; i++) { |
Elliott Hughes | 3b3e118 | 2011-10-06 13:53:01 -0700 | [diff] [blame] | 28 | threads[i] = new MyThread(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Elliott Hughes | 3b3e118 | 2011-10-06 13:53:01 -0700 | [diff] [blame] | 31 | 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 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 40 | 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 Hughes | 3b3e118 | 2011-10-06 13:53:01 -0700 | [diff] [blame] | 65 | static int mCount = 0; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 66 | public void run() { |
Elliott Hughes | 3b3e118 | 2011-10-06 13:53:01 -0700 | [diff] [blame] | 67 | synchronized (MyThread.class) { |
| 68 | ++mCount; |
| 69 | } |
jeffhao | 94d6df4 | 2012-11-26 16:02:12 -0800 | [diff] [blame^] | 70 | try { |
| 71 | sleep(1000); |
| 72 | } catch (Exception ex) { |
| 73 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | class 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 | } |