Created
February 14, 2014 03:15
-
-
Save jarrodhroberson/8995193 to your computer and use it in GitHub Desktop.
How to determine how many threads you can create from Java
This file contains hidden or 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
public class TestThreadStackSizes | |
{ | |
public static void main(final String[] args) | |
{ | |
Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { | |
public void uncaughtException(final Thread t, final Throwable e) | |
{ | |
System.err.println(e.getMessage()); | |
System.exit(1); | |
} | |
}); | |
int numThreads = 1000; | |
if (args.length == 1) | |
{ | |
numThreads = Integer.parseInt(args[0]); | |
} | |
for (int i = 0; i < numThreads; i++) | |
{ | |
try | |
{ | |
Thread t = new Thread(new SleeperThread()); | |
t.start(); | |
} | |
catch (final OutOfMemoryError e) | |
{ | |
throw new RuntimeException(String.format("Out of Memory Error on Thread %d", i), e); | |
} | |
} | |
} | |
private static class SleeperThread implements Runnable | |
{ | |
public void run() | |
{ | |
try | |
{ | |
Thread.sleep(1000 * 60 * 60); | |
} | |
catch (final InterruptedException e) | |
{ | |
throw new RuntimeException(e); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment