并发和并行有什么区别?
赞赏的例子.
Java中是否有良好的延续实现?
如果是这样,那么开销是多少?JVM的设计并没有考虑到这些,对吧?那种反对谷物的是什么?
在使用 Kotlin 进行编码时,是选择其中一种更好还是另一种更好?
该视频:Java 21 新功能:虚拟线程 #RoadTo21似乎不赞成将虚拟线程用于非 IO 或非阻塞任务。
我什至在 Kotlin 代码中为 CPU 密集型任务创建了左右协程。这已经不行了吗?
我正在试验Kotlin 协程并有以下代码:
fun main(args: Array<String>) = runBlocking {
val cores = Runtime.getRuntime().availableProcessors()
println("number of cores: $cores")
val jobs = List(10) {
async(CommonPool) {
delay(100)
println("async #$it on thread ${Thread.currentThread().name}")
}
}
jobs.forEach { it.join() }
}
Run Code Online (Sandbox Code Playgroud)
这是我的输出:
number of cores: 4
async number:0 on thread ForkJoinPool.commonPool-worker-2
async number:2 on thread ForkJoinPool.commonPool-worker-3
async number:3 on thread ForkJoinPool.commonPool-worker-3
async number:4 on thread ForkJoinPool.commonPool-worker-3
async number:5 on thread ForkJoinPool.commonPool-worker-3
async number:1 on thread ForkJoinPool.commonPool-worker-1
async number:7 on thread ForkJoinPool.commonPool-worker-3
async number:6 …Run Code Online (Sandbox Code Playgroud) 协程有两种类型。堆叠和无堆叠。而 Kotlin 协程是无栈协程。
另一方面,一旦一个方法被调用,它就会被堆叠在内存中。我们可以使用方法进行递归调用。
在 Kotlin 中,我需要做的就是suspend为协程上下文中调用的方法添加关键字。
它没有堆栈,那么它是如何工作的?
我的猜测是,由于协程对象属于某个线程,因此该线程拥有它们。让我们说,是的。那么它是如何在引擎盖下工作的呢?