Skip to content

Instantly share code, notes, and snippets.

@hgdeoro
Created August 25, 2011 21:24
Show Gist options
  • Save hgdeoro/1172016 to your computer and use it in GitHub Desktop.
Save hgdeoro/1172016 to your computer and use it in GitHub Desktop.
ThreadStress
public class ThreadStress {
public static class SomeRunnable implements Runnable {
public long num = 0;
long time;
public SomeRunnable(int time) {
this.time = time;
}
@Override
public void run() {
final long start = System.currentTimeMillis();
while (System.currentTimeMillis() - start < time) {
num++;
}
}
}
private static void test(int cantThreads) throws InterruptedException {
SomeRunnable runnables[] = new SomeRunnable[cantThreads];
Thread threads[] = new Thread[runnables.length];
for (int i = 0; i < runnables.length; i++) {
runnables[i] = new SomeRunnable(20000);
threads[i] = new Thread(runnables[i]);
}
for (int i = 0; i < runnables.length; i++) {
// System.out.println("Iniciando " + i);
threads[i].start();
}
long total = 0;
for (int i = 0; i < runnables.length; i++) {
threads[i].join();
// System.out.println("runnables[i].num=" + runnables[i].num);
total += runnables[i].num;
}
System.out.println("Total con " + runnables.length + " threds:" + total);
}
public static void main(String[] args) throws InterruptedException {
test(512);
test(1024);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment