Skip to content

Instantly share code, notes, and snippets.

@michael-trelinski
Created September 19, 2013 08:32
Show Gist options
  • Save michael-trelinski/6620629 to your computer and use it in GitHub Desktop.
Save michael-trelinski/6620629 to your computer and use it in GitHub Desktop.
package com.satoricode.forker;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ForkIt {
public static void main(String[] args) throws InterruptedException {
int arraySize = Integer.parseInt(args[0]);
System.out.print("Filling up some memory... ");
int[] arr = fillItUp(arraySize);
System.out.println("" + arr.length + " done.");
if (args.length == 1) {
System.out.println("I'm the fork!");
Thread.sleep(10 * 1000);
} else {
// i'm the master
int numForks = Integer.parseInt(args[1]);
String stackCommand = args[2];
String stackInitCommand = args[3];
ExecutorService pool = Executors.newFixedThreadPool(numForks);
System.out.println("Launching " + numForks + " forks with array size " + arraySize + " with -" + stackCommand + " -" + stackInitCommand);
for(int i = 0; i < numForks; i++)
pool.execute(new ForkRunner(arraySize, stackCommand, stackInitCommand));
pool.shutdown();
System.out.print("Waiting for pool to finish");
while(!pool.isTerminated()) {
Thread.sleep(50);
System.out.print(".");
}
System.out.println();
System.out.println("done\n");
}
}
private static int[] fillItUp(int total) {
int[] arr = new int[total];
for (int i = 0; i < total; i++)
arr[i] = i;
return arr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment