OldThreadGroupTest.java revision ab56f644a3d1a42658d4ec34650f7b882e29e374
12333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson/*
22333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson *  Licensed to the Apache Software Foundation (ASF) under one or more
32333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson *  contributor license agreements.  See the NOTICE file distributed with
42333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson *  this work for additional information regarding copyright ownership.
52333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson *  The ASF licenses this file to You under the Apache License, Version 2.0
62333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson *  (the "License"); you may not use this file except in compliance with
72333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson *  the License.  You may obtain a copy of the License at
82333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson *
92333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson *     http://www.apache.org/licenses/LICENSE-2.0
102333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson *
112333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson *  Unless required by applicable law or agreed to in writing, software
122333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson *  distributed under the License is distributed on an "AS IS" BASIS,
132333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
142333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson *  See the License for the specific language governing permissions and
152333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson *  limitations under the License.
162333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson */
172333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
182333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilsonpackage libcore.java.lang;
192333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
20ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilsonimport java.util.ArrayList;
21ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilsonimport java.util.Arrays;
22ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilsonimport java.util.HashSet;
23ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilsonimport java.util.List;
24ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilsonimport java.util.Set;
252333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilsonimport junit.framework.TestCase;
262333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
272333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilsonpublic class OldThreadGroupTest extends TestCase implements Thread.UncaughtExceptionHandler {
282333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
292333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    class MyThread extends Thread {
302333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        public volatile int heartBeat = 0;
312333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
322333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        public MyThread(ThreadGroup group, String name)
332333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                throws SecurityException, IllegalThreadStateException {
342333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            super(group, name);
352333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        }
362333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
372333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        @Override
382333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        public void run() {
392333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            while (true) {
402333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                heartBeat++;
412333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                try {
422333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                    Thread.sleep(50);
432333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                } catch (InterruptedException e) {
442333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                    break;
452333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                }
462333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            }
472333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        }
482333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
492333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        public boolean isActivelyRunning() {
502333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            long MAX_WAIT = 100;
512333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            return isActivelyRunning(MAX_WAIT);
522333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        }
532333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
542333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        public boolean isActivelyRunning(long maxWait) {
552333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            int beat = heartBeat;
562333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            long start = System.currentTimeMillis();
572333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            do {
582333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                Thread.yield();
592333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                int beat2 = heartBeat;
602333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                if (beat != beat2) {
612333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                    return true;
622333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                }
632333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            } while (System.currentTimeMillis() - start < maxWait);
642333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            return false;
652333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        }
662333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
672333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    }
682333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
692333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    private ThreadGroup initialThreadGroup = null;
702333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
712333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    public void test_activeGroupCount() {
722333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        ThreadGroup tg = new ThreadGroup("group count");
732333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertEquals("Incorrect number of groups",
742333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                0, tg.activeGroupCount());
752333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        Thread t1 = new Thread(tg, new Runnable() {
762333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            public void run() {
772333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
782333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            }
792333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        });
802333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertEquals("Incorrect number of groups",
812333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                0, tg.activeGroupCount());
822333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        t1.start();
832333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertEquals("Incorrect number of groups",
842333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                0, tg.activeGroupCount());
852333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        new ThreadGroup(tg, "test group 1");
862333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertEquals("Incorrect number of groups",
872333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                1, tg.activeGroupCount());
882333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        new ThreadGroup(tg, "test group 2");
892333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertEquals("Incorrect number of groups",
902333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                2, tg.activeGroupCount());
912333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    }
922333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
932333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    @SuppressWarnings("deprecation")
942333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    public void test_allowThreadSuspensionZ() {
952333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        ThreadGroup tg = new ThreadGroup("thread suspension");
962333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertTrue("Thread suspention can not be changed",
972333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                tg.allowThreadSuspension(false));
982333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertTrue("Thread suspention can not be changed",
992333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                tg.allowThreadSuspension(true));
1002333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    }
1012333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
1022333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    /*
1032333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson     * Checks whether the current Thread is in the given list.
1042333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson     */
1052333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    private boolean inListOfThreads(Thread[] threads) {
1062333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        for (int i = 0; i < threads.length; i++) {
1072333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            if (Thread.currentThread() == threads[i]) {
1082333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                return true;
1092333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            }
1102333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        }
1112333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
1122333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        return false;
1132333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    }
1142333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
1152333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    public void test_enumerateLThreadArray() {
1162333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        int numThreads = initialThreadGroup.activeCount();
1172333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        Thread[] listOfThreads = new Thread[numThreads];
1182333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
1192333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        int countThread = initialThreadGroup.enumerate(listOfThreads);
1202333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertEquals(numThreads, countThread);
1212333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertTrue("Current thread must be in enumeration of threads",
1222333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                inListOfThreads(listOfThreads));
1232333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    }
1242333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
125ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson    public void test_enumerateLThreadArrayLZtest_enumerateLThreadArrayLZ() throws Exception {
126ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        // capture the initial condition
127ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        int initialThreadCount = initialThreadGroup.activeCount();
128ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        Thread[] initialThreads = new Thread[initialThreadCount];
129ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        assertEquals(initialThreadCount, initialThreadGroup.enumerate(initialThreads, false));
130ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        assertEquals(initialThreadCount, initialThreadGroup.enumerate(initialThreads, true));
131ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        assertTrue(inListOfThreads(initialThreads));
132ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson
133ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        // start some the threads and see how the count changes
134ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        ThreadGroup group = new ThreadGroup(initialThreadGroup, "enumerateThreadArray");
135ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        int groupSize = 3;
136ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        List<MyThread> newThreads = populateGroupsWithThreads(group, groupSize);
137ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        assertEquals(initialThreadCount, initialThreadGroup.enumerate(initialThreads, true));
138ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        assertTrue(inListOfThreads(initialThreads));
139ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        for(MyThread thread : newThreads) {
140ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson            thread.start();
1412333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        }
142ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        Thread.sleep(500); // starting threads isn't instant!
143ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        int afterStartCount = initialThreadGroup.activeCount();
144ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        Set<Thread> initialPlusNew = new HashSet<Thread>();
145ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        initialPlusNew.addAll(Arrays.asList(initialThreads));
146ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        initialPlusNew.addAll(newThreads);
147ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        Thread[] afterStartThreads = new Thread[afterStartCount];
148ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        assertEquals(afterStartCount, initialThreadGroup.enumerate(afterStartThreads, true));
149ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        assertEquals(initialPlusNew, new HashSet<Thread>(Arrays.asList(afterStartThreads)));
150ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        assertTrue(inListOfThreads(afterStartThreads));
151ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson
152ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        // kill the threads and count 'em again
153ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        for(MyThread thread : newThreads) {
154ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson            thread.interrupt();
1552333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        }
156ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        Thread.sleep(500); // killing threads isn't instant
157ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        int afterDeathCount = initialThreadGroup.activeCount();
158ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        Thread[] afterDeathThreads = new Thread[afterDeathCount];
159ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        assertEquals(afterDeathCount, initialThreadGroup.enumerate(afterDeathThreads, false));
160ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        assertEquals(Arrays.asList(initialThreads), Arrays.asList(afterDeathThreads));
161ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        assertTrue(inListOfThreads(afterDeathThreads));
1622333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    }
1632333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
1642333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    public void test_enumerateLThreadGroupArray() {
1652333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        int numGroupThreads = initialThreadGroup.activeGroupCount();
1662333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        ThreadGroup[] listOfGroups = new ThreadGroup[numGroupThreads];
1672333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
1682333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        int countGroupThread = initialThreadGroup.enumerate(listOfGroups);
1692333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertEquals(numGroupThreads, countGroupThread);
1702333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
1712333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        ThreadGroup[] listOfGroups1 = new ThreadGroup[numGroupThreads + 1];
1722333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        countGroupThread = initialThreadGroup.enumerate(listOfGroups1);
1732333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertEquals(numGroupThreads, countGroupThread);
1742333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertNull(listOfGroups1[listOfGroups1.length - 1]);
1752333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
1762333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        ThreadGroup[] listOfGroups2 = new ThreadGroup[numGroupThreads - 1];
1772333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        countGroupThread = initialThreadGroup.enumerate(listOfGroups2);
1782333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertEquals(numGroupThreads - 1, countGroupThread);
1792333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
1802333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        ThreadGroup thrGroup1 = new ThreadGroup("Test Group 1");
1812333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        countGroupThread = thrGroup1.enumerate(listOfGroups);
1822333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertEquals(0, countGroupThread);
1832333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson     }
1842333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
1852333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    public void test_enumerateLThreadGroupArrayLZ() {
1862333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        ThreadGroup thrGroup = new ThreadGroup("Test Group 1");
187ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        List<MyThread> subThreads = populateGroupsWithThreads(thrGroup, 3);
1882333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        int numGroupThreads = thrGroup.activeGroupCount();
1892333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        ThreadGroup[] listOfGroups = new ThreadGroup[numGroupThreads];
1902333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
1912333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertEquals(0, thrGroup.enumerate(listOfGroups, true));
1922333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertEquals(0, thrGroup.enumerate(listOfGroups, false));
1932333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
1942333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        for(MyThread thr:subThreads) {
1952333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            thr.start();
1962333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        }
1972333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
1982333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        numGroupThreads = thrGroup.activeGroupCount();
1992333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        listOfGroups = new ThreadGroup[numGroupThreads];
2002333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
2012333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertEquals(0, thrGroup.enumerate(listOfGroups, true));
2022333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertEquals(0, thrGroup.enumerate(listOfGroups, false));
2032333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
2042333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        ThreadGroup subGroup1 = new ThreadGroup(thrGroup, "Test Group 2");
205ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        List<MyThread> subThreads1 = populateGroupsWithThreads(subGroup1, 3);
2062333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        numGroupThreads = thrGroup.activeGroupCount();
2072333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        listOfGroups = new ThreadGroup[numGroupThreads];
2082333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
2092333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertEquals(1, thrGroup.enumerate(listOfGroups, true));
2102333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertEquals(1, thrGroup.enumerate(listOfGroups, false));
2112333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
2122333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        for(MyThread thr:subThreads1) {
2132333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            thr.start();
2142333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        }
2152333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        numGroupThreads = thrGroup.activeGroupCount();
2162333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        listOfGroups = new ThreadGroup[numGroupThreads];
2172333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
2182333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertEquals(1, thrGroup.enumerate(listOfGroups, true));
2192333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertEquals(1, thrGroup.enumerate(listOfGroups, false));
2202333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
2212333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        for(MyThread thr:subThreads) {
2222333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            thr.interrupt();
2232333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson         }
2242333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
2252333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        ThreadGroup subGroup2 = new ThreadGroup(subGroup1, "Test Group 3");
226ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        List<MyThread> subThreads2 = populateGroupsWithThreads(subGroup2, 3);
2272333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        numGroupThreads = thrGroup.activeGroupCount();
2282333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        listOfGroups = new ThreadGroup[numGroupThreads];
2292333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
2302333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertEquals(2, thrGroup.enumerate(listOfGroups, true));
2312333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertEquals(1, thrGroup.enumerate(listOfGroups, false));
2322333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    }
2332333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
2342333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    /**
2352333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson     * @tests java.lang.ThreadGroup#interrupt()
2362333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson     */
2372333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    private static boolean interrupted = false;
2382333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    public void test_interrupt() {
2392333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
2402333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        Thread.setDefaultUncaughtExceptionHandler(this);
2412333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        ThreadGroup tg = new ThreadGroup("interrupt");
2422333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        Thread t1 = new Thread(tg, new Runnable() {
2432333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            public void run() {
2442333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                try {
2452333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                    Thread.sleep(5000);
2462333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                } catch (InterruptedException e) {
2472333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                    fail("ok");
2482333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                }
2492333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            }
2502333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        });
2512333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertFalse("Incorrect state of thread", interrupted);
2522333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        t1.start();
2532333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertFalse("Incorrect state of thread", interrupted);
2542333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        t1.interrupt();
2552333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        try {
2562333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            t1.join();
2572333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        } catch (InterruptedException e) {
2582333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        }
2592333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertTrue("Incorrect state of thread", interrupted);
2602333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        tg.destroy();
2612333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    }
2622333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
2632333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    public void test_isDestroyed() {
2642333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        final ThreadGroup originalCurrent = getInitialThreadGroup();
2652333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        final ThreadGroup testRoot = new ThreadGroup(originalCurrent,
2662333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                "Test group");
2672333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertFalse("Test group is not destroyed yet",
2682333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                testRoot.isDestroyed());
2692333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        testRoot.destroy();
2702333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertTrue("Test group already destroyed",
2712333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                testRoot.isDestroyed());
2722333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    }
2732333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
2742333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    @SuppressWarnings("deprecation")
2752333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    public void test_resume() {
2762333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        ThreadGroup group = new ThreadGroup("Foo");
2772333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
2782333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        Thread thread = launchFiveSecondDummyThread(group);
2792333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
2802333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        try {
2812333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            Thread.sleep(1000);
2822333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        } catch (InterruptedException e) {
2832333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            // Ignore
2842333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        }
2852333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
2862333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        // No-op in Android. Must neither have an effect nor throw an exception.
2872333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        Thread.State state = thread.getState();
2882333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        group.resume();
2892333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        assertEquals(state, thread.getState());
2902333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    }
2912333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
2922333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    private Thread launchFiveSecondDummyThread(ThreadGroup group) {
2932333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        Thread thread = new Thread(group, "Bar") {
2942333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            public void run() {
2952333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                try {
2962333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                    Thread.sleep(5000);
2972333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                } catch (InterruptedException e) {
2982333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                    // Ignore
2992333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                }
3002333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            }
3012333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        };
3022333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
3032333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        thread.start();
3042333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
3052333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        return thread;
3062333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    }
3072333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
3082333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    /*
3092333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson     * @see java.lang.Thread.UncaughtExceptionHandler#uncaughtException(java.lang.Thread, java.lang.Throwable)
3102333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson     */
3112333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    public void uncaughtException(Thread t, Throwable e) {
3122333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        interrupted = true;
3132333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        Thread.setDefaultUncaughtExceptionHandler(null);
3142333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    }
3152333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
3162333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    @Override
3172333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    protected void setUp() {
3182333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        initialThreadGroup = Thread.currentThread().getThreadGroup();
3192333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        ThreadGroup rootThreadGroup = initialThreadGroup;
3202333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        while (rootThreadGroup.getParent() != null) {
3212333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            rootThreadGroup = rootThreadGroup.getParent();
3222333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        }
3232333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    }
3242333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
3252333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    @Override
3262333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    protected void tearDown() {
3272333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        try {
3282333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            // Give the threads a chance to die.
3292333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            Thread.sleep(50);
3302333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        } catch (InterruptedException e) {
3312333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        }
3322333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    }
3332333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
3342333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    private ThreadGroup getInitialThreadGroup() {
3352333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        return initialThreadGroup;
3362333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    }
3372333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
3382333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    private ThreadGroup[] groups(ThreadGroup parent) {
339ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        // No API to get the count of immediate children only
3402333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        int count = parent.activeGroupCount();
3412333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        ThreadGroup[] all = new ThreadGroup[count];
3422333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        parent.enumerate(all, false);
3432333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        // Now we may have nulls in the array, we must find the actual size
3442333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        int actualSize = 0;
3452333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        for (; actualSize < all.length; actualSize++) {
3462333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            if (all[actualSize] == null) {
3472333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson                break;
3482333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson            }
3492333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        }
350ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        return Arrays.copyOfRange(all, 0, actualSize);
3512333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    }
3522333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
353ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson    private List<MyThread> populateGroupsWithThreads(ThreadGroup group, int threadCount) {
354ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        List<MyThread> result = new ArrayList<MyThread>();
355ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        populateGroupsWithThreads(group, threadCount, result);
3562333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        return result;
3572333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    }
3582333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
359ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson    private void populateGroupsWithThreads(ThreadGroup group, int threadCount, List<MyThread> out) {
3602333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        for (int i = 0; i < threadCount; i++) {
361ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson            out.add(new MyThread(group, "MyThread " + i + " of " + threadCount));
3622333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        }
3632333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson
3642333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        // Recursively for subgroups (if any)
365ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson        ThreadGroup[] children = groups(group);
3662333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        for (ThreadGroup element : children) {
367ab56f644a3d1a42658d4ec34650f7b882e29e374Jesse Wilson            populateGroupsWithThreads(element, threadCount, out);
3682333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson        }
3692333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson    }
3702333d6b20eed39cfac75edaf9643aaf543251537Jesse Wilson}
371