Coroutines in Java?

M

Mike Amling

Is it possible to implement coroutines
(http://en.wikipedia.org/wiki/Coroutine) in a single Java Thread? I
couldn't think of any way to do it at the source code level. I'm not all
that familiar with bytecode, but there isn't, for instance, a way to get
the JVM to use a second calling stack, is there?
I can see plenty of ways to implement coroutines using more than one
Thread.

--Mike Amling
 
J

Joshua Cranmer

Mike said:
Is it possible to implement coroutines
(http://en.wikipedia.org/wiki/Coroutine) in a single Java Thread? I
couldn't think of any way to do it at the source code level. I'm not all
that familiar with bytecode, but there isn't, for instance, a way to get
the JVM to use a second calling stack, is there?
No.

I can see plenty of ways to implement coroutines using more than one
Thread.

Indeed.
 
R

Roedy Green

Is it possible to implement coroutines
(http://en.wikipedia.org/wiki/Coroutine) in a single Java Thread? I
couldn't think of any way to do it at the source code level. I'm not all
that familiar with bytecode, but there isn't, for instance, a way to get
the JVM to use a second calling stack, is there?
I can see plenty of ways to implement coroutines using more than one
Thread.

Long ago I wrote a way to simulate multiple threads on windows 3.1
with a single thread using C. It required each task to periodically
call a "have a conscience" method to allow some other thread a chance
to run. Coroutines would be a miniature version of this where two
methods cooperatively share the same thread.

As I recall I would capture the stack state and swap in a different
one on each context switch. That is too low level for Java to let you
do that, though you probably could do it with JNI and a particular JVM
with not much optimisation going on.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Species evolve exactly as if they were adapting as best they could to a changing world, and not at all as if they were moving toward a set goal."
~ George Gaylord Simpson
 
T

Tronje Krop

Hi Mike,
Is it possible to implement coroutines
(http://en.wikipedia.org/wiki/Coroutine) in a single Java Thread? I
couldn't think of any way to do it at the source code level. I'm not all
that familiar with bytecode, but there isn't, for instance, a way to get
the JVM to use a second calling stack, is there?
I can see plenty of ways to implement coroutines using more than one
Thread.

May be you should also have a look at continuations when
looking for coroutines, as they provide the basic mechanism
for implementing coroutines:

http://en.wikipedia.org/wiki/Continuation

What is not written on this page: Java is currently prepared
to support continuations in the Da Vinci VM Project:

http://wikis.sun.com/display/mlvm/StackContinuations

I think that is what you have asked for. There was some rumor
about getting them in Java 7, but I'm not sure about that.

Beside this low-level (native) Java implementation there are
a number of high-level Java tools out in the wild providing
some kind of coroutines. One I can recommend is also noted
on the related topic of actors:

http://en.wikipedia.org/wiki/Actor_model

It is called "Kilim". Unfortunately, the link is misleading.
Better look here:

http://www.malhar.net/sriram/kilim/

It provides the basic mechanisms by weaving the necessary
instructions into bytecode using a small set of annotations
for markup.

Best regards,

Tronje
 
S

Seamus MacRae

Tronje said:
What is not written on this page: Java is currently prepared
to support continuations in the Da Vinci VM Project:

http://wikis.sun.com/display/mlvm/StackContinuations

I think that is what you have asked for. There was some rumor
about getting them in Java 7, but I'm not sure about that.

Support for these at the VM level could also allow for a new
exception-handling option: a "resume" keyword usable in catch clauses
that does exactly what it says on the tin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top