Thursday, October 11, 2007

Generator (with yield) in Java

For some reason I was thinking about generators and the yield function (as found in Python). All this functional talk about infinite lists and such I suppose... So I had a go at implementing one in Java, which tied in nicely with my current reflexions about a java-based functional language and the need to provide concurrency out of the box. And another occasion to dabble with wait and notify in Java! After I've written it I found a similar looking implementation (but without the standard Iterator/Iterable interface support) and something on Google code that use bytecode intrumentation (why oh why). But still I like mine, that you can find here (and the unit test is here).
Basically the work is done in another thread and we call wait() when we do a yield. We conform to the Iterator and the Iterable interfaces, and we provide a stop method to actually stop the thread (say for infinite generators). Otherwise if the generator has not finished the thread goes on waiting forever. I've thought about trying to stop the thread when the generator is not used, but I don't know if it even possible with things like SoftReferences (The thread having a pointer to its runnable...).

4 comments:

Keith Houston said...

Hi there - I was looking for an implementation of coroutines in Java recently, and I tried out your class. I like it!

The one change I made was to delay starting the thread until either hasNext() or next() (whichever is called first) so that derived classes can initialise member variables from constructor arguments before the thread tries to read them.

Anyway, nice work!

Pierre said...

Hi!

I was looking for such an implementation and stumbled upon your page but i can't seem to be able to access the URL for the class nor the tests.

Did you move them around?

Thanks!

JP Moresmau said...

Links fixed. Yes I moved the data to some other server.

Pierre said...

Thanks for your prompt response and nice job with the implementation! :)