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 { |
Brian Carlstrom | cbaf987 | 2014-02-10 14:15:56 -0800 | [diff] [blame] | 24 | System.out.println("thread test starting"); |
| 25 | testThreadCapacity(); |
| 26 | testThreadDaemons(); |
| 27 | testSleepZero(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 28 | System.out.println("thread test done"); |
| 29 | } |
| 30 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 31 | /* |
| 32 | * Simple thread capacity test. |
| 33 | */ |
Brian Carlstrom | cbaf987 | 2014-02-10 14:15:56 -0800 | [diff] [blame] | 34 | private static void testThreadCapacity() throws Exception { |
| 35 | TestCapacityThread[] threads = new TestCapacityThread[512]; |
| 36 | for (int i = 0; i < 512; i++) { |
| 37 | threads[i] = new TestCapacityThread(); |
| 38 | } |
| 39 | |
| 40 | for (TestCapacityThread thread : threads) { |
| 41 | thread.start(); |
| 42 | } |
| 43 | for (TestCapacityThread thread : threads) { |
| 44 | thread.join(); |
| 45 | } |
| 46 | |
| 47 | System.out.println("testThreadCapacity thread count: " + TestCapacityThread.mCount); |
| 48 | } |
| 49 | |
| 50 | private static class TestCapacityThread 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() { |
Brian Carlstrom | cbaf987 | 2014-02-10 14:15:56 -0800 | [diff] [blame] | 53 | synchronized (TestCapacityThread.class) { |
Elliott Hughes | 3b3e118 | 2011-10-06 13:53:01 -0700 | [diff] [blame] | 54 | ++mCount; |
| 55 | } |
jeffhao | 94d6df4 | 2012-11-26 16:02:12 -0800 | [diff] [blame] | 56 | try { |
| 57 | sleep(1000); |
| 58 | } catch (Exception ex) { |
| 59 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 62 | |
Brian Carlstrom | cbaf987 | 2014-02-10 14:15:56 -0800 | [diff] [blame] | 63 | private static void testThreadDaemons() { |
| 64 | Thread t = new Thread(null, new TestDaemonThread(), "TestDaemonThread", 7168); |
| 65 | |
| 66 | t.setDaemon(false); |
| 67 | |
| 68 | System.out.print("testThreadDaemons starting thread '" + t.getName() + "'\n"); |
| 69 | t.start(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 70 | |
| 71 | try { |
Brian Carlstrom | cbaf987 | 2014-02-10 14:15:56 -0800 | [diff] [blame] | 72 | t.join(); |
| 73 | } catch (InterruptedException ex) { |
| 74 | ex.printStackTrace(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Brian Carlstrom | cbaf987 | 2014-02-10 14:15:56 -0800 | [diff] [blame] | 77 | System.out.print("testThreadDaemons finished\n"); |
| 78 | } |
| 79 | |
| 80 | private static class TestDaemonThread implements Runnable { |
| 81 | public void run() { |
| 82 | System.out.print("testThreadDaemons @ Thread running\n"); |
| 83 | |
| 84 | try { |
| 85 | Thread.currentThread().setDaemon(true); |
| 86 | System.out.print("testThreadDaemons @ FAILED: setDaemon() succeeded\n"); |
| 87 | } catch (IllegalThreadStateException itse) { |
| 88 | System.out.print("testThreadDaemons @ Got expected setDaemon exception\n"); |
| 89 | } |
| 90 | |
| 91 | try { |
| 92 | Thread.sleep(2000); |
| 93 | } |
| 94 | catch (InterruptedException ie) { |
| 95 | System.out.print("testThreadDaemons @ Interrupted!\n"); |
| 96 | } |
| 97 | finally { |
| 98 | System.out.print("testThreadDaemons @ Thread bailing\n"); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private static void testSleepZero() throws Exception { |
| 104 | Thread.currentThread().interrupt(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 105 | try { |
Brian Carlstrom | cbaf987 | 2014-02-10 14:15:56 -0800 | [diff] [blame] | 106 | Thread.sleep(0); |
| 107 | throw new AssertionError("unreachable"); |
| 108 | } catch (InterruptedException e) { |
| 109 | if (Thread.currentThread().isInterrupted()) { |
| 110 | throw new AssertionError("thread is interrupted"); |
| 111 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 112 | } |
Brian Carlstrom | cbaf987 | 2014-02-10 14:15:56 -0800 | [diff] [blame] | 113 | System.out.print("testSleepZero finished\n"); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 114 | } |
| 115 | } |