|
1 /* |
|
2 * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 6379235 |
|
27 * @run main/othervm -server -Xmx32m -Xms32m -Xss256m StartOOMTest |
|
28 * @summary ThreadGroup accounting mistake possible with failure of Thread.start() |
|
29 */ |
|
30 |
|
31 import java.util.*; |
|
32 |
|
33 public class StartOOMTest |
|
34 { |
|
35 public static void main(String[] args) throws Throwable { |
|
36 Runnable r = new SleepRunnable(); |
|
37 ThreadGroup tg = new ThreadGroup("buggy"); |
|
38 List<Thread> threads = new ArrayList<Thread>(); |
|
39 Thread failedThread; |
|
40 int i = 0; |
|
41 for (i = 0; ; i++) { |
|
42 Thread t = new Thread(tg, r); |
|
43 try { |
|
44 t.start(); |
|
45 threads.add(t); |
|
46 } catch (Throwable x) { |
|
47 failedThread = t; |
|
48 System.out.println(x); |
|
49 System.out.println(i); |
|
50 break; |
|
51 } |
|
52 } |
|
53 |
|
54 int j = 0; |
|
55 for (Thread t : threads) |
|
56 t.interrupt(); |
|
57 |
|
58 while (tg.activeCount() > i/2) |
|
59 Thread.yield(); |
|
60 failedThread.start(); |
|
61 failedThread.interrupt(); |
|
62 |
|
63 for (Thread t : threads) |
|
64 t.join(); |
|
65 failedThread.join(); |
|
66 |
|
67 try { |
|
68 Thread.sleep(1000); |
|
69 } catch (Throwable ignore) { |
|
70 } |
|
71 |
|
72 int activeCount = tg.activeCount(); |
|
73 System.out.println("activeCount = " + activeCount); |
|
74 |
|
75 if (activeCount > 0) { |
|
76 throw new RuntimeException("Failed: there should be no active Threads in the group"); |
|
77 } |
|
78 } |
|
79 |
|
80 static class SleepRunnable implements Runnable |
|
81 { |
|
82 public void run() { |
|
83 try { |
|
84 Thread.sleep(60*1000); |
|
85 } catch (Throwable t) { |
|
86 } |
|
87 } |
|
88 } |
|
89 } |