i**p 发帖数: 902 | | i**p 发帖数: 902 | 2 怎么做到的?
A function marked with this keyword can suspend a coroutine execution, but
is not blocking the thread.
https://www.kotlindevelopment.com/deep-dive-coroutines/
【在 i**p 的大作中提到】 : 怎么有的文章说没创建新的thread.
| L*********s 发帖数: 3063 | 3 创建新的thread还叫什么coroutine啊, 创建新的thread实现"coroutine"不是脱裤子放
屁?
【在 i**p 的大作中提到】 : 怎么有的文章说没创建新的thread.
| n******t 发帖数: 4406 | 4 因為大家就喜歡這這種名字啊。。lol
【在 L*********s 的大作中提到】 : 创建新的thread还叫什么coroutine啊, 创建新的thread实现"coroutine"不是脱裤子放 : 屁?
| i**p 发帖数: 902 | 5 知道就说说怎么实现的,不知道就潜着。
插科打诨请到菌斑。
【在 L*********s 的大作中提到】 : 创建新的thread还叫什么coroutine啊, 创建新的thread实现"coroutine"不是脱裤子放 : 屁?
| h*i 发帖数: 3446 | 6 CSP (communicating sequential processes)
Put plainly, like guvest said, these are all games of goto. "CSP", "
coroutine", "suspend", "park", "yield", "await" etc, whatever word they use,
it means the similar things: save the context, pass the PC elsewhere,
switch back when ready. The detailed differences are academic only.
These are often implemented in the language, not in OS, so it can be "
lightweight", so could be faster. But it may not always be so, because you
can implement CSP with OS threads.
e.g. in Clojure, CSP is implemented as a library via macro (these macros
compile the async code into state machines, eventually to a jump table,
hence a game of goto), backed up by a thread pool. In Clojurescript, CSP is
implemented the same, but Javascript does not have threads, so you basically
have one thread to play the same game.
In non-lisp languages, the same game is played, but it is not implemented as
a library, but in the language itself.
Javascript's async-await, kotlin coroutine, go Goroutine, etc, they are all
similar.
Anyone tells you that theirs is better are either idiots or language zealots
. Ignore them.
【在 i**p 的大作中提到】 : 怎么做到的? : A function marked with this keyword can suspend a coroutine execution, but : is not blocking the thread. : https://www.kotlindevelopment.com/deep-dive-coroutines/
| h*i 发帖数: 3446 | 7 Did a bit reading, kotlin's coroutines and go's goroutines are also backed
by OS threads in the end.
So basically, they added a scheduler/dispatcher to allocate g/coroutine to
threads. And these threads are from a thread pool with a fixed size (
configurable on system startup)
Not different from how Clojure does it. | h*i 发帖数: 3446 | 8 Contrary to most places' warnings, my suggestion to you is to use thousands
of threads for your CSP thread pool. They recommend you to use the same
number of threads as your CPU cores, don't listen to these fools.
Because modern programming is mostly doing IO, you will be blocked on IO
everywhere. If your thread pool is not large enough, you will run out of
threads with heavy load.
Modern Linux machines handles thousands of threads just fine. Compared with
IO, threading context switch costs almost nothing at all.
【在 h*i 的大作中提到】 : Did a bit reading, kotlin's coroutines and go's goroutines are also backed : by OS threads in the end. : So basically, they added a scheduler/dispatcher to allocate g/coroutine to : threads. And these threads are from a thread pool with a fixed size ( : configurable on system startup) : Not different from how Clojure does it.
| h*i 发帖数: 3446 | 9 thread pool
the threads are already created. they are reused.
【在 i**p 的大作中提到】 : 怎么有的文章说没创建新的thread.
| L*********s 发帖数: 3063 | 10 看https://www.kotlindevelopment.com/deep-dive-coroutines/
《What's inside?》那节,
我是不可能解释得比它好了。
【在 i**p 的大作中提到】 : 知道就说说怎么实现的,不知道就潜着。 : 插科打诨请到菌斑。
| i**p 发帖数: 902 | 11 确实还是在用thread支持coroutine的运行,有创建新thread的选项。
Coroutine context includes a coroutine dispatcher (see CoroutineDispatcher)
that determines what thread or threads the corresponding coroutine uses for
its execution. Coroutine dispatcher can confine coroutine execution to a
specific thread, dispatch it to a thread pool, or let it run unconfined.
https://kotlinlang.org/docs/reference/coroutines/coroutine-context-and-
dispatchers.html
【在 h*i 的大作中提到】 : thread pool : the threads are already created. they are reused.
| i**p 发帖数: 902 | 12 费解的是这个例子,println运行在 main, 同一个 thread
launch { // context of the parent, main runBlocking coroutine
println("main runBlocking : I'm working in thread ${Thread.
currentThread().name}")
}
main runBlocking : I'm working in thread main
【在 h*i 的大作中提到】 : thread pool : the threads are already created. they are reused.
| h*i 发帖数: 3446 | 13 it's entirely possible that they use one thread.
CSP has nothing to do with threads. It's about a game of goto. Turn your
code into a bunch of goto, each with the right context. That's it.
Remember, in Javascript, there's only one thread, you can still do CSP.
【在 i**p 的大作中提到】 : 费解的是这个例子,println运行在 main, 同一个 thread : launch { // context of the parent, main runBlocking coroutine : println("main runBlocking : I'm working in thread ${Thread. : currentThread().name}") : } : main runBlocking : I'm working in thread main
|
|