Wrapping Up: Using the ParallelArray API
By now, a couple of things about the ParallelArray API design should become more obvious. First, the ParallelArray class and its kin are written in a fluent API style. That means you can string operations together simply by chaining method calls, where each successive method call is operating on the returned collection from the prior call. So the returned collection from sort()
is the set to which apply()
calls the Ops.Procedure instance's op()
Second, the original contents of the ParallelArray are never modified. This is a huge advantage as it lessens any concerns about thread safety. That is, you don't have to worry about synchronizing access to the ParallelArray because generally you need to set up locks only when modifying the contents, not reading them. (This same principle is why strings are immutable.)
Continuing with the demo, you can do various things with the ParallelArray. For example, you can determine which students are supposed to be graduating this year and display them. This is a filter operation that requires an Ops.Predicate instance, which this time returns a Boolean indicating whether or not the object in question should be included in the results:
// . . .
System.out.println("Should graduate this year:");
// . . .
private static final Ops.Predicate isSenior =
public boolean op(Student s)
return s.graduationYear == 2009; // this year
The initial criterion is just to find out whether the students are supposed to graduate this year, but remember that not all who attend college graduate college. Students have to attain a certain grade point average in order to earn those coveted diplomas. One solution would be to write a new Ops.Predicate that takes both criteria into account, but that's going to lead to an explosion of predicates if you're not careful. The chaining-based approach of the ParallelArray API allows for a better solution (see Listing 2).
This particular university, like most universities, is particularly hard on athletes to make sure they're not just there to look good on TV. (Yes, that's my little revenge on all those guys who graduated from my college with 0.5 GPAs because they threw 30 touchdowns and won the national championships. Not that I'm still bitter or anything.)