Getting Started: The Domain Problem
Using the fork/join framework happens at several different levels, but in many respects the easiest way to get started with the framework is with the ParallelArray class. When to use the fork/join framework obviously is a decision best made only with the full context of the situation at hand, but in general consider fork/join for searching, sorting, summarizing, and filtering data sets. For example, consider a simple student grading system, using a Student domain class similar to the one in
Listing 1.
Observant readers will notice that Listing 1 uses public fields. In production code, you obviously would replace it with the appropriate bean methods, but the class fulfils the pedagogical purposes of this example just fine (Click here to download the complete source code).
The domain problem in Listing 1 is simple: a small college/university has a number of students, all of which the system needs to be able to sort, select, and display. (Of course the problem is tailor made for the fork/join frameworkdemos have that tendency.) To begin, you first need to generate a number of random students. Use a helper method to create a number of students in an array:
import jsr166y.forkjoin.*;
public class ForkJoin
{
private static Student[] generateStudents(int classSize)
{ /* ,,, */ }
public static void main(String[] args)
{
assert (args.length < 1);
int classSize = new Integer(args[0]);
assert (classSize > 0);
Student[] currentClass = generateStudents(classSize);
System.out.println("Random seed data:");
for (Student s : currentClass)
{
System.out.println("\t" + s);
}
System.out.println("");
// . . .
}
The actual details of the generateStudents() method is unimportant for this demo. It simply generates a bunch of Student objects.