import java.util.Random; import java.util.concurrent.ForkJoinPool; /** * */ /** * In this example, we have an array filled with numbers ranging from 0 to 99. * We want to count how many of the numbers are from 0-9, 10-19, etc.... 90-99. * @author sbfan * */ public class HistogramRunner { /** * @param args */ public static void main(String[] args) { // creating our input array, filled with ints from 0-99 int[] input = new int[99999]; Random r = new Random(); for(int i = 0; i < input.length; i++) { input[i] = r.nextInt(100); } // now we will try to create a histogram, counting up // the # of ints that are 0-9, 10-19, etc. int bins = 10; // only 10 possible bins, 0-9, 10-19, ... 90-99. int[] results = new int[bins]; // sequential way for(int i = 0; i < input.length; i++ ) { results[input[i] / bins] += 1; // increment count of correct bin } System.out.println("sequential results:"); // Print results for(int i = 0; i < results.length; i++) { System.out.println("Bin " + i + ": " + results[i]); } // parallel way ForkJoinPool fjp = new ForkJoinPool(); results = fjp.invoke(new Histogram(input, 0, input.length)); System.out.println("parallel results:"); // Print results for(int i = 0; i < results.length; i++) { System.out.println("Bin " + i + ": " + results[i]); } } }