Tag: 协同程序

JavaFx / Kotlin中的多线程图像呈现

我试图尽可能快地使用所有的CPU核心来填充图像(使用JavaFx),但是我遇到了一些问题。 下面是我可以想出的最小的例子,重现了这个问题。 当我用launch函数创建的Job上调用join()时,图像渲染正确,但显然只使用一个CPU内核? 这是为什么? 另一方面,当我没有在返回的作业上调用join()时,所有的CPU核心都被利用了,但是显然,图像渲染是不正确的,因为父方法不会等待作业完成: 任何方式,我可以填补所有内核,仍然看起来是正确的? package com.serious.business import javafx.animation.AnimationTimer import javafx.application.Application import javafx.scene.Scene import javafx.scene.canvas.Canvas import javafx.scene.image.WritableImage import javafx.scene.layout.Pane import javafx.stage.Stage import kotlinx.coroutines.experimental.CommonPool import kotlinx.coroutines.experimental.launch import kotlinx.coroutines.experimental.runBlocking class ImageTestApp: Application() { val w = 800.0 val h = 800.0 val image = WritableImage(w.toInt(), h.toInt()) var blue: Double = 0.0 override fun start(primaryStage: Stage) { […]

返回Kotlin协同程序中产生的值

我正在尝试返回从协程生成的值 fun nonSuspending (): MyType { launch(CommonPool) { suspendingFunctionThatReturnsMyValue() } //Do something to get the value out of coroutine context return somehowGetMyValue } 我想出了以下解决方案(不是很安全!): fun nonSuspending (): MyType { val deferred = async(CommonPool) { suspendingFunctionThatReturnsMyValue() } while (deferred.isActive) Thread.sleep(1) return deferred.getCompleted() } 我也想过使用事件总线,但有没有更好的解决这个问题? 提前致谢。

并行S3文件通过Kotlin协程上传

我需要上传许多文件到S3,这将需要数小时才能顺利完成这项工作。 这正是Kotlin的新协程所擅长的,所以我想给他们一个尝试,而不是再次用一些基于线程的执行服务来摆弄。 这是我的(简体)代码: fun upload(superTiles: Map<Int, Map<Int, SuperTile>>) = runBlocking { val s3 = AmazonS3ClientBuilder.standard().withRegion("eu-west-1").build() for ((x, ys) in superTiles) { val jobs = mutableListOf<Deferred<Any>>() for ((y, superTile) in ys) { val job = async(CommonPool) { uploadTile(s3, x, y, superTile) } jobs.add(job) } jobs.map { it.await() } } } suspend fun uploadTile(s3: AmazonS3, x: Int, […]

Kotlin – 异常后继续协程

我的问题 异常之后是否可以继续执行协程? 例 try { someMethod1() someMethod2() //Throws an exception! someMethod3() } catch(e: Exception) { //I do not want to call someMethod3 here! //I want the coroutine to resume after the exception inside of the original block. } finally { //I also do not want to call someMethod3 here! //I want the coroutine to resume […]

在Kotlin中,我如何传递一个参数,使异步范围能够节约呢?

我有下面的代码片断使用Kotlin协程 fun main(args:Array<String>){ println("test") var seed = 3 val deferredResult = async(CommonPool){ seed * 2 } seed = 4 runBlocking(CommonPool) { val result = deferredResult.await() println("Result is $result") } println("end") } 我期待它的行为像JavaScript,并在定义协程时保存seed变量(使用副本)的价值。 但不是打印Result is 6 ,它打印Result is 8 。 我能做些什么来确保在异步范围(而不是4)内使用seed变量(即3)的原始值?

如何:在Kotlin中启动并忘记异步协程

我一直在阅读Kotlin的例程,但没有找到具体问题的答案。 假设我想迭代一个集合,为每个元素进行API调用(在这种情况下,将文件推送到Amazon S3)。 我希望这些调用被异步协程处理,以免在等待时阻塞底层线程。 我不需要从请求返回值,只是为了记录异常。 我将如何创建一个“消防和忘记”异步协程来做出这些请求之一?

Kotlin:阻塞具有非阻塞I / O的协程

我正在尝试使用Kotlin协同处理非阻塞I / O。 情景如下: 从线程1上运行的异步回调接收数据。 等待线程2中的这些数据,然后将其消耗。 我现在的代码看起来像这样(为简洁起见而简化): private var latch = CountDownLatch(1) private var data: Any? = null // Async callback from non-blocking I/O fun onReceive(data: Any) { currentData = data latch.countDown() } // Wait and consume data fun getData(): Any? { latch.await() latch = CountDownLatch(1) return currentData } fun processData() { launch(CommonPool) { while […]

如何使用Kotlin协程等待()在主线程上

我刚开始学习Kotlin协同程序,并试图模拟一些长时间的API调用,并在UI上显示结果: class MainActivity : AppCompatActivity() { fun log(msg: String) = println("[${Thread.currentThread().name}] $msg") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) this.setContentView(R.layout.activity_main) val resultTV = findViewById(R.id.text) as TextView val a = async(CommonPool) { delay(1_000L) 6 } val b = async(CommonPool) { delay(1_000L) 7 } launch(< NEED UI thread here >) { val aVal = a.await() val bVal = […]

Kotlin中线程与协程的区别

Kotlin中是否有任何特定的语言实现与其他语言的协同程序有所不同? 这意味着协程就像轻量级的线程? 有什么不同? kotlin的协同程序是否并行/同时运行? 即使在多核系统中,在任何给定的时间只有一个协程运行(是不是?) 这里我开始了100000个协程,这个代码背后发生了什么? for(i in 0..100000){ async(CommonPool){ //run long running operations } }

Kotlin Coroutines是Android的正确方法

我正在尝试使用异步更新适配器内的列表,我可以看到有太多的样板。 这是使用Kotlin协同程序的正确方法吗? 这可以优化更多? fun loadListOfMediaInAsync() = async(CommonPool) { try { //Long running task adapter.listOfMediaItems.addAll(resources.getAllTracks()) runOnUiThread { adapter.notifyDataSetChanged() progress.dismiss() } } catch (e: Exception) { e.printStackTrace() runOnUiThread {progress.dismiss()} } catch (o: OutOfMemoryError) { o.printStackTrace() runOnUiThread {progress.dismiss()} } }