简体   繁体   中英

What's the relationship between nio and coroutine in java?

My understanding is, in Java, coroutine is based on nio. So somehow I can say java support coroutine. I can use selector/channel to implement my coroutine app myself. Am I right?

You can have 1 thread deal with N connections, but that is not implementing coroutines; it's only juggling.

At best the code will be async-aware, split itself in before/after phases and launching reads/writes asynchronously. It will have to hookup some form of callback handlers, scheduled tasks, reactive streams, etc, or phases of a selectable polling loop to resume processing, but obviously not at the same line of code.

There is no way in java (without major jvm/bytecode hacks) that a thread can leave and return transparently to a method's blocking/yielding point to do something else.

(This is all assuming that you are intending to save threads, not waste them into some form of coroutine emulation with further blocking wait/notify. One can obviously just park the calling thread until an async task finally notifies it; that's not winning any performance.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM