« January 2007 »
SunMonTueWedThuFriSat
 
1
2
3
5
6
7
8
11
13
14
16
17
18
20
21
22
23
25
26
27
28
29
30
31
   
       
Today
XML

Blog::Navigation

GetJava Download Button
Get the Source
Personal Blog

Blog::Referers

Today's Page Hits: 1860

Powered by Roller Weblogger.
« Previous day (Jan 17, 2007) | Main | Next day (Jan 19, 2007) »
20070119 Friday January 19, 2007

Continuations for Java

Continuation is an object that represents the execution state of a program at a certain point. We can use continuation to restart the execution from the point stored in it.

How about continuations for the Java platform? There are atleast two different implementations of continuations:

I've experimented with javaflow. I checked out javaflow sources under, say %JAVAFLOW_HOME% directory (no pre-built binaries available in the site). I tried building it by maven. But failed ... because I need parent pom file! (never mind if you don't understand that -- that is just a build step). Because I am only interested in playing with continuation, I ignored maven build. I just created a NetBeans project and added all source directories of javaflow. I copied dependent libraries (ant.jar, commons-logging-1.0.4.jar, junit-3.8.2.bar, junit-addons-1.4.jar -- I just copied the versions that I had -- check for proper dependency or use maven to build!) under %JAVAFLOW_HOME%\lib directory and added all jars under this directory to netbeans project. I managed to build and produce javaflow.jar under %JAVAFLOW_HOME%\dist. The following is a simple program that uses continuations [this is just slightly modified version of the one in the javaflow tutorial]


import org.apache.commons.javaflow.*;

class Test {
  static class MyRunnable implements Runnable {
    public void run() {
      System.out.println("run started!");
      for( int i=0; i < 10; i++ ) {
        echo(i);
      }
    }

    private void echo(int x) {
      System.out.println("echo " + x);
      Continuation.suspend();
    }
  }

  public static void main(String[] args) {
    System.out.println("main started");
    Continuation c = Continuation.startWith(new MyRunnable());
    System.out.println("in main after continuation return");
    while (c != null) {
       c = Continuation.continueWith(c);
       System.out.println("in main");   
    }
  }
}

The output of the above program is shown below:
main started
run started!
echo 0
in main after continuation return
echo 1
in main
echo 2
in main
echo 3
in main
echo 4
in main
echo 5
in main
echo 6
in main
echo 7
in main
echo 8
in main
echo 9
in main
in main

The execution seems to "flip-flop" between Test.main and Test.MyRunnable.echo methods!. No, there are no multiple threads here. Single thread of execution produces the output shown above. That is because of continuation. If you don't get that, you may want to read this.

The steps used in build, run the above program:

As you may know already, JDK 6 includes javax.script API and Mozilla Rhino based JavaScript engine. There is continuation support in Rhino. But then, that is a topic for another blog entry :-)



( Jan 19 2007, 08:55:23 PM IST ) Permalink Comments [10] del.icio.us | furl | simpy | slashdot | technorati | digg

Copyright (C) 2005, A. Sundararajan's Weblog