简体   繁体   English

nio和协程在Java中是什么关系?

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

My understanding is, in Java, coroutine is based on nio. 我的理解是,在Java中,协程基于nio。 So somehow I can say java support coroutine. 所以我可以说Java支持协程。 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; 您可以让1个线程处理N个连接,但是这没有实现协程。 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. 在Java中(没有主要的jvm / bytecode hacks),线程无法离开并透明地返回到方法的阻塞/屈服点以执行其他操作。

(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.) (所有这些都假设您打算保存线程,而不是通过进一步阻塞等待/通知来将它们浪费在某种形式的协程仿真中。很明显,您可以将调用线程驻留在异步任务最终通知它之前;这并不能赢得任何性能。 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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