Skip to content

Instantly share code, notes, and snippets.

@vorburger
Created November 2, 2017 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vorburger/3d8c9d6b87f61d6baffef9e1874ce13f to your computer and use it in GitHub Desktop.
Save vorburger/3d8c9d6b87f61d6baffef9e1874ce13f to your computer and use it in GitHub Desktop.
Java class which creates A LOT of threads
/*
* 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