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