Created
September 26, 2023 11:39
-
-
Save indigo423/e3072ab91025ab0740fe5e52f69cc816 to your computer and use it in GitHub Desktop.
Test maximum number of Java 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
import java.util.ArrayList; | |
import java.util.List; | |
public class Main { | |
public static final int BATCH_SIZE = 4000; | |
public static void main(String[] args) { | |
List<Thread> threads = new ArrayList<Thread>(); | |
try { | |
for (int i = 0; i <= 100 * 1000; i += BATCH_SIZE) { | |
long start = System.currentTimeMillis(); | |
addThread(threads, BATCH_SIZE); | |
long end = System.currentTimeMillis(); | |
Thread.sleep(1000); | |
long delay = end - start; | |
System.out.printf("%,d threads: Time to create %,d threads was %.3f seconds %n", threads.size(), BATCH_SIZE, delay / 1e3); | |
} | |
} catch (Throwable e) { | |
System.err.printf("After creating %,d threads, ", threads.size()); | |
e.printStackTrace(); | |
} | |
} | |
private static void addThread(List<Thread> threads, int num) { | |
for (int i = 0; i < num; i++) { | |
Thread t = new Thread(new Runnable() { | |
@Override | |
public void run() { | |
try { | |
while (!Thread.interrupted()) { | |
Thread.sleep(1000); | |
} | |
} catch (InterruptedException ignored) { | |
// | |
} | |
} | |
}); | |
t.setDaemon(true); | |
t.setPriority(Thread.MIN_PRIORITY); | |
threads.add(t); | |
t.start(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment