JavaOne 2008 - Closures Cookbook

10:09 pm Java

The session “Closures Cookbook” was presented by Neal Gafter of Google. Gafter is one of the proposers of the BGGA closures spec which is currently under review via JSR-166y to add closures to the Java 7 language.

A closure is an object represented by a code body and a lexical context. It’s an instance of an interface with very few restrictions. It can access local variables and “this”, and may return a value. Here’s a trivial example of the syntax in the BGGA closure spec:

Runnable r = {=> doWhatever();};
r.run();

One of the big benefits of closures is that you can replace large amounts of “boiler plate” code required to implement some interfaces (i.e. the same code needs to be written the same way in many places in an an application for such functionality as logging or a timing).

Another benefit is performing bulk computations over data in aggregates (arrays, lists, maps, etc.).  These computations are automatically parallelized.  Examples of these include sort, filter, map, reduce, and fold.

Closures also allow a developer to take advantage of concurrency without needing to code for it.

This session used many code examples, making it difficult to blog, but I will be sure to post the presentation slides as soon as they become available.

Leave a Comment

Your comment

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.