Skip to main content

This Time It's Real

Posted by editor on August 25, 2006 at 6:35 AM PDT

Will closures finally make it into Java?

When I attended a session on Ruby at last year's ApacheCon, I came away not particuarly interested in Rails, but wildly jealous of Ruby's implementation of closures. Blame it on the desktop programmer in me, particularly all the times I've had to create one-off threads to get some action on to or off of the event-dispatch thread. The problem is when you need some inner thread to access a local variable. Consider the following scenario:

  String userInput = JOptionPane.showInputDialog (...);
  // let's say we need to do something with
  // userInput that takes a long time, like
  // looking up a network service or hitting a DB.
  // we'll thread the call so we don't block the GUI.
  Thread oneOffThread = new Thread() {
    public void run() {
      // do something with userInput
      // ...
      // make a SwingUtilities.invokeLater() call
      // to update GUI with results

To the casual reader, this seems just fine. The only problem is that it doesn't compile, because the Thread's run() method can't see the value of the local variable userInput. The typical workaround is something of an ugly hack: declaring userInput to be final. Of course, sometimes the variable can't be made final -- maybe you need to modify userInput before sending it to the thread -- so you have to create some other variable, a final that exists for the sole purpose of having userInput's value copied into it, making it visible to the Thread. Or make userInput an instance variable, even though it's only used in this method.

Can you say ugly? Heck, this is fugly.

This is a problem closures are well-suited to solve. Creating the thread inside a closure would give it access to all the local variables in the method, in their state at the exact moment the closure is created, with no need to create finals that exist only to expose the local values to the thread.

Interestingly, it's not like closures were just proposed last week out of a sudden bout of Ruby envy. In fact, Artima's Bill Venners has rounded up several previous calls to bring closures to Java. In Seeking the Joy in Java, he cites Bruce Tate, who called closures the number-one thing Java should steal from Ruby, and Gilad Bracha, who says he's been advocating closures in Java since 1997. In fact, Bill points out Patrick Naughton's The Long Strange Trip to Java, which recounts an "epic" argument in which Naughton and James Gosling battled with Bill Joy, who wanted closures in the Oak language, Java's predecessor, back in 1994.

With this topic on the minds of many, the latest Poll asks "Would you like JDK 7 to support closures?" Cast your vote on the front page, then visit the results page for current tallies and discussion.

Also in Java Today,
according to a recent announcement, the NetBeans Mobility Pack has opened its code to the community. has detailed info about the Mobility Pack, including downloads of the latest builds, design documents, APIs, roadmaps and partner information. Further information is available in the news release Sun Open Sources Java Mobile Edition Development Tool.

For those looking to get started with the JDK 6 sources, Ivan Tarasov has a step-by-step introduction in Try this at home: Building the JDK (on Debian). "One of my assignments here at Sun is to build the latest sources available at the Peabody site and provide some useful directions on how to do it. I use Debian Linux both at work and at home on my laptop, so I'll describe the building process on Debian. Probably some day I'll try to do it on another OS, say Windows or Mac OS X, when I get my hands on the computer with this OS installed. So, here we go..."

dgilbert argues that the GPL might be the best defense against bad forks of open-source Java. The Forum message Re: Copyleft, GPL, and Java says:
"The GPL has worked well for Linux, and I think it can work well for the Java platform too. One of the nice features of the GPL is that it discourages 'bad' forking, because forks must be done out in the open (reducing the economic incentives of proprietary forks), and licensing barriers to recombining forked code are eliminated. Sun would have to make a lot of clarifying statements to allay people's GPL fears, but that's something they can handle. I'd be surprised if the GPL is not one of the top-ranked licences on Sun's list right now."

In Applet or Webstart...that is the question, mezmo is dealing with a browser integration problem: "I've got a requirement to get Outlook Web access play nice with the rest of my portal. So, I've got a little button that pops up a window and loads owa and logs the user on. Well, a little problem popped up when the user logged out, its taking every browser session there is with it. So we work with M$ and they say that we have to force the other browser session into another process, that will take care of the problem. So I write a little applet that does a runtime.exec and that works least on my pc. So I look up the instructions on making it work in other browsers, including public average everyday computer users, and I'm seeing nightmare city! Import certificates and policy tools, OH MY!!! So, am I barking up the wrong tree trying to do this in an applet? Will a web start app work the same way, only just pop up a little dialog that will ask the user if its ok for me to run? Am I just wasting my time and I should just go find a friggin VB programmer to write an activeX to do this with? I'd really hate to have to do that."

Today's Weblogs also touch on the question of closures in Java. In Closure and performance, Rémi