My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* Copyright (c) 2011. Peter Lawrey
*
* "THE BEER-WARE LICENSE" (Revision 128)
* As long as you retain this notice you can do whatever you want with this stuff.
* If we meet some day, and you think this stuff is worth it, you can buy me a beer in return
* There is no warranty.
*/

package com.google.code.java.core.threads;

import java.util.ArrayList;
import java.util.List;

public class MaxThreadsMain {

public static final int BATCH_SIZE = 4000;

public static void main(String... args) throws InterruptedException {
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();
}
}
}

Change log

r58 by peter.lawrey on Aug 30, 2011   Diff
Beerware licensing.
Go to: 
Project members, sign in to write a code review

Older revisions

r26 by peter.lawrey on Jul 18, 2011   Diff
Add maximum threads test.
All revisions of this file

File info

Size: 1650 bytes, 57 lines
Powered by Google Project Hosting