offbynull / coroutines

Java toolkit that allows you to write coroutines.
GNU Lesser General Public License v3.0
346 stars 53 forks source link

Coroutine and Synchonized blocks inside. #80

Closed publicocean0 closed 6 years ago

publicocean0 commented 6 years ago

My idea is to use many coroutines instead to use many threads because it is more scalable. Consider i realize a scheduler in which i can push many coroutines. Pratically it is a coroutines pool inside the same thread(in a more generic model the coroutine pool is over a pool of threads running , threads number is equals to cpus in the machine).

When a corotuine is blocked by a wait() instruction(inside a synchronized block) it is not necessary to block the thread but it is sufficient to suspend the coroutine. When a corotuine is suspended , it is taken another coroutine ready to be resumed. In this way the thread is never blocked ... and the global performance will be very hight.

For this reason is important to instrument synchronized blocks. I read notes in your page that you instrument synchonized blocks. Do you instrument synchononized blocks for suspending/resuming coroutines automatically?

offbynull commented 6 years ago

Yes. If your coroutine suspends inside of synchronized blocks, those locks will automatically be unlocked when you suspend and re-locked when you continue.

However, I'm almost 100% sure your experiment won't give you the results you're expecting. Normal thread operations are already incredibly fast.

The real power of this project is the ability to serialize and version, not to speed up execution. Other people have tried to use coroutines to speed up. There's a framework called Quasar you can look at. It's still maintained but it makes some questionable claims and never really took off.

Quasar's author now works on Java. He's trying to make coroutines a built-in language feature. I'm trying to help with that effort, but it remains to be seen if that ends up happening.

On Jan 20, 2018 1:07 PM, "publicocean0" notifications@github.com wrote:

My idea is to use many coroutines instead to use many threads because it is more scalable. Consider i realize a scheduler in which i can push many coroutines. Pratically it is a coroutines pool inside the same thread.

When a corotuine is blocked by a wait() instruction(inside a synchronized block) it is not necessary to block the thread but it is sufficient to suspend the coroutine. When a corotuine is suspended , it is taken another coroutine ready to be resumed. In this way the thread is never blocked ... and the global performance will be very hight.

For this reason is important to instrument synchronized blocks. I read notes in your page that you instrument synchonized blocks. Do you instrument synchononized blocks for suspending/resuming coroutines automatically?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/offbynull/coroutines/issues/80, or mute the thread https://github.com/notifications/unsubscribe-auth/AGD4Ikto9vEF34_SYH3SMW9Wqaxs4f7eks5tMlWBgaJpZM4RllgP .

publicocean0 commented 6 years ago

I already tried quasar it is great but it has a problem. The synchonized , notify and wait methods are not instrumented so it is not really usable if you use many libraries except if you replace manually all synchonized locks with locks for fibers in all your sources. Using locks for fiber you did exactly what i want do.... but replace all code manually is not possible (i use also external libraries) My code is a intensive software receiving a tons of concurrent requests so i wontt use a simple thread pool. The normal locks are consuming a lot of time in this context. Using my idea i think this time is not consumed. If coroutines will be built in java language it will be cool , that surelly will solve also my problem. But when it will be available? is there a experimental jdk for trying it?

offbynull commented 6 years ago

Work has just started on it.

There is no code yet. There is no timeline for when it will be available.

There is no guarantee that it will even actually make it into Java.

publicocean0 commented 6 years ago

The problem in java is JDK is not so open to community for improvements. In the last years there were many languages trying to overcome Java(kotlin,golang,...).Sun and OpenJDK might ask ourselves why? It is sad to see a so big java developers community is so binded to a so closed JDK.It get slow the evolution of new tecnologies. If JDK would be placed in github now we will have a language completely different :) Anyway i can t change this problem. I m evaluating to patch the instrumenter of a coroutine library or quasar directly. To developer the scheduler for my opinion is not so difficult. The just worrying part for me is to patch instrumenter replacing in coroutine block all obj.notify/obj.wait with scheduler.notify(obj) /scheduler.wait(obj). I will have to studiy asm and aboveall internals in coroutine and to begin to pray that code inside instrumenter doesnt get me crazy :) Tomorrow i will go to church ahaha