Created
November 2, 2017 23:39
-
-
Save vorburger/3d8c9d6b87f61d6baffef9e1874ce13f to your computer and use it in GitHub Desktop.
Java class which creates A LOT of threads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved. | |
* | |
* This program and the accompanying materials are made available under the | |
* terms of the Eclipse Public License v1.0 which accompanies this distribution, | |
* and is available at http://www.eclipse.org/legal/epl-v10.html | |
*/ | |
package test; | |
/** | |
* Example creating a lot of threads. | |
* | |
* This is handled "orderly" by the JVM, it doesn't just kill: | |
* | |
* Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread | |
* at java.lang.Thread.start0(Native Method) | |
* at java.lang.Thread.start(Thread.java:717) | |
* at test.ThreadExplosion.main(ThreadExplosion.java:25) | |
* | |
* For https://jira.opendaylight.org/browse/NETVIRT-974 | |
* | |
* @author Michael Vorburger.ch | |
*/ | |
public class ThreadExplosion { | |
public static void main(String[] args) { | |
for (int i = 0; i < 1_000_000; i++) { | |
new Thread((Runnable) () -> { | |
try { | |
Thread.sleep(1_000_000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
}, "TestThread" + i).start(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment