Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Forking and Joining Java to Maximize Multicore Power : Page 4

With JSR-166y, Java 7 will get a new way of structuring concurrent programming known as the fork/join framework. Find out how (and when) to use it.


advertisement

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() method.

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:"); processor .withFilter(isSenior) .apply(printOutStudent); System.out.println(""); // . . . } private static final Ops.Predicate isSenior = new Ops.Predicate() { 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.)



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap