Path: blob/master/test/jdk/java/lang/Thread/StartOOMTest.java
41149 views
/*1* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* This test is relatively useful for verifying 6379235, but25* is too resource intensive, especially on 64 bit systems,26* to be run automatically, see 6721694.27*28* When run it should be typically be run with the server vm29* and a relatively small java heap, and a large stack size30* ( to provoke the OOM quicker ).31* java -server -Xmx32m -Xms32m -Xss256m StartOOMTest32*/3334import java.util.*;3536public class StartOOMTest37{38public static void main(String[] args) throws Throwable {39Runnable r = new SleepRunnable();40ThreadGroup tg = new ThreadGroup("buggy");41List<Thread> threads = new ArrayList<Thread>();42Thread failedThread;43int i = 0;44for (i = 0; ; i++) {45Thread t = new Thread(tg, r);46try {47t.start();48threads.add(t);49} catch (Throwable x) {50failedThread = t;51System.out.println(x);52System.out.println(i);53break;54}55}5657int j = 0;58for (Thread t : threads)59t.interrupt();6061while (tg.activeCount() > i/2)62Thread.yield();63failedThread.start();64failedThread.interrupt();6566for (Thread t : threads)67t.join();68failedThread.join();6970try {71Thread.sleep(1000);72} catch (Throwable ignore) {73}7475int activeCount = tg.activeCount();76System.out.println("activeCount = " + activeCount);7778if (activeCount > 0) {79throw new RuntimeException("Failed: there should be no active Threads in the group");80}81}8283static class SleepRunnable implements Runnable84{85public void run() {86try {87Thread.sleep(60*1000);88} catch (Throwable t) {89}90}91}92}939495