10

I am looking for recent work presenting continuations in Java. I have come across the same question here but it dates back for a year or two.

There is some work such as JavaFlow by Apache, RIFE Continuations (that I cannot download for any reason now). Also there is a blog post mentioning the support in JDK but it seems that the support will span to Java 8. I also reckon that continuations are also introduced in recent Scala versions.

I am looking for implementations in Java presenting the continuations concept. And, I am not looking for the works that present continuation-passing-style (CSP).

I'd be thankful for any other work you might know.

Community
  • 1
  • 1
nobeh
  • 9,784
  • 10
  • 49
  • 66
  • I edited the post, is it clear now? – nobeh Sep 17 '11 at 19:28
  • In Java, there is usually a better way to solve a problem than using co-routines or continuations. This is partly because Java doesn't support it. – Peter Lawrey Sep 17 '11 at 19:56
  • 1
    @PeterLawrey Well, I may not fully agree to you since they're planning this programming feature for JDK 8. And, in terms of multicore programming and suspending some code the continuations seem to be needed. Please correct me if I'm wrong. – nobeh Sep 18 '11 at 16:20
  • 1
    Continuation can be useful and may me included in Java 8. However I think their main benefit is light weight threading which you can control. As you have more cores, it actually makes it easier to throw hardware and threads at the problem instead. – Peter Lawrey Sep 18 '11 at 19:36

3 Answers3

1

It's not clear from your post why JavaFlow wouldn't meet your needs. JYeild is another library and I'd start with an Apache project before try a project like JYeild with less support.

Chip McCormick
  • 744
  • 4
  • 17
0

we recently added first-class continuations to kilim and have a 2.0 pre-release up.

http://github.com/nqzero/kilim

here's a snipped that calculates a XorShift (num times):

    public static class X2 extends Continuation implements Loop {
    long result;
    public void execute() throws Pausable {
        long x, y, s0=103, s1=17;
        while (true) {
            x = s0;
            y = s1;
            s0 = y;
            x ^= (x << 23);
            s1 = x ^ y ^ (x >> 17) ^ (y >> 26);
            result = (s1 + y);
            Fiber.yield();
        }
    }
    public long loop(long num) {
        long val = 0;
        for (int ii=0; ii < num && !run(); ii++)
            val = val ^ result;
        return val;
    }
}

a higher level facility is also provided (called Task) that gets automatically scheduled, eg because a network packet arrives or a file read completes

nqzero
  • 125
  • 1
  • 7
0

Checkout recently released experimental support of coroutines in Kotlin

For example code snippet below shows that coroutines in Koltin are actually light-weight threads. Creating 100000 normal threads would most likely cause some serious error like OutOfMemoryError, but this code works just fine:

fun main(args: Array<String>) = runBlocking<Unit> {
    val jobs = List(100_000) { // create a lot of coroutines and list their jobs
        launch(CommonPool) {
            delay(1000L)
            print(".")
        }
    }
    jobs.forEach { it.join() } // wait for all jobs to complete
}

Of course there is no big magic here and Kotlin will just generate some code for you at compile time which will execute all those 100000 tasks in a ForkJoinPool.

There are many more cool examples in the documentation.

Andrey
  • 194
  • 1
  • 7
  • Please include the essential parts of the answer from the link here and provide the link for reference. Link-only answers can become invalid if the linked page changes – ivpavici Mar 06 '17 at 09:57