Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coroutines in Java

Tags:

java

coroutine

I am reading this page about coroutines in Python and this Wikipedia page. I saw that there are a few libraries in Java implementing coroutines.

My question is: is there any known reason why the Java designers decided not to implement coroutines so far and is there any plan to include it in a future version of Java?

Thanks.

like image 717
joel314 Avatar asked Apr 12 '16 12:04

joel314


People also ask

Will Java get coroutines?

Java, as slow adopter of new concepts is getting structured concurrency as part of the Loom project. This addition enables native support for coroutines (termed virtual threads) in Java.

What is the use of coroutines?

Coroutines provide us an easy way to do synchronous and asynchronous programming. Coroutines allow execution to be suspended and resumed later at some point in the future which is best suited for performing non-blocking operations in the case of multithreading. Coroutines were added to Kotlin in version 1.1.

Can we use Kotlin coroutines in Java?

We can consume this API from Kotlin coroutine to load two images and combine then asynchronously. The resulting function returns CompletableFuture<Image> for ease of use back from Java. Note that this module should be used only for integration with existing Java APIs based on CompletableFuture .

Are coroutines the same as threads?

Coroutines are lighter than threads . Since they stack less . I.e coroutines don't have a dedicated stack . It means coroutine suspend execution by returning to the caller and the data that is required to resume execution is stored separately from the stack.


3 Answers

Actually the concept of a co-routine was the first design of the Java threading system. The wait/notify mechanism is a simplistic form of co-routine where notify is equivalent to yield etc.

Since then much has been done, particularly to make structures thread-safe rather than algorithms. This derives from the realization that it is not the code that must synchronize/yield but the data structure used to communicate between the threads that must be thread-safe.

like image 104
OldCurmudgeon Avatar answered Sep 24 '22 12:09

OldCurmudgeon


Project Loom

Continuations and Coroutines will come to Java in the nearer future and they’ll be called virtual threads (also referred to as fibers). There’s a project called Loom:

Project Loom is intended to explore, incubate and deliver Java VM features and APIs built on top of them for the purpose of supporting easy-to-use, high-throughput lightweight concurrency and new programming models on the Java platform. This is accomplished by the addition of the following constructs:

  • Virtual threads
  • Delimited continuations
  • Tail-call elimination

Further reading: https://cr.openjdk.java.net/~rpressler/loom/Loom-Proposal.html

To quote that document:

It is the goal of this project to add a public delimited continuation (or coroutine) construct to the Java platform. However, this goal is secondary to fibers …

Preliminary builds of Project Loom are available now, based on early-access Java 16.

like image 32
s1m0nw1 Avatar answered Sep 21 '22 12:09

s1m0nw1


On the "are there any plans ..." part of the question, the answer is:

Not at this stage

The JEP list (http://openjdk.java.net/jeps/0) does not make any mention of coroutines. The list covers features added in Java 8, added or targeted for Java 9, or proposed for future releases.

Interestingly, there was an RFE submitted in March 2013 (https://bugs.openjdk.java.net/browse/JDK-8029988). The RFE only got one vote, and it was closed 9 months with the suggestion to submit a JEP. Nobody has bothered to take the idea any further, which to me is telling.

like image 33
Stephen C Avatar answered Sep 23 '22 12:09

Stephen C