Tag: kotlinx.coroutines

如何在kotlin中编写递归协程

我想写一个更快的文件搜索器在kotlin找到我的主文件夹下的所有文本文件。 这是我的代码序列搜索 import java.io.File import java.util.concurrent.atomic.AtomicLong var count: AtomicLong = AtomicLong(0) fun main(args: Array<String>) { println(System.getProperty("user.home")) val start = System.currentTimeMillis() findTxtFile(System.getProperty("user.home") + "/git", ".txt") println("took: " + (System.currentTimeMillis() – start)) println(count.get()) } fun findTxtFile(path: String, extension: String) { File(path).listFiles().forEach { if (it.isDirectory) { findTxtFile(it.absolutePath, extension) } else if (it.name.endsWith(extension)) { count.getAndIncrement() } } } 我下面写了一个程序,但是当你有很多文件的时候,会抛出NullPointerException,怎么可能抛出空指针异常呢? […]

Kotlin协程和Spring Framework 5反应类型

Kotlin协程允许通过返回Deferred值来执行非阻塞代码。 这对于在使用阻塞方法(例如从库)中生成非阻塞代码非常有用。 Spring 5允许在框架中使用Mono和Flux 。 我所看到的最大兴趣是能够序列化这两种类型的实例,并在有人呼叫控制器端点时作为响应发回。 Spring 5的一个重要特点是对Kotlin(路由器,bean声明,…)有特定的支持,但是我找不到有关Kotlin协同程序和Spring 5反应类型之间可能的相互作用的信息。 有什么办法可以将这些功能的优点结合起来吗? 将Deferred转换为Mono / Flux ? 有一个Deferred作为响应类型的Spring控制器方法? 如果否,那么在这种情况下,如果我们有Spring 5反应类型,那么协程是否有意义呢?

单元测试Kotlin协同程序延迟

我试图单元测试使用delay()的Kotlin协同程序。 对于单元测试,我不在乎delay() ,只是减慢测试速度。 我想以某种方式运行测试,当delay()被调用时实际上不会延迟。 我尝试使用委托给CommonPool的自定义上下文运行协程: class TestUiContext : CoroutineDispatcher(), Delay { suspend override fun delay(time: Long, unit: TimeUnit) { // I'd like it to call this } override fun scheduleResumeAfterDelay(time: Long, unit: TimeUnit, continuation: CancellableContinuation<Unit>) { // but instead it calls this } override fun dispatch(context: CoroutineContext, block: Runnable) { CommonPool.dispatch(context, block) } } 我希望我可以从我的上下文的delay()方法返回,而是调用我的scheduleResumeAfterDelay()方法,我不知道如何将其委派给默认调度程序。

Vertx plus Kotlin协程永远挂起

我正在使用Kotlin协同程序重写一些Java Vertx异步代码以用于学习目的。 但是,当我尝试测试一个简单的HTTP调用时,基于协程的测试永远挂起,我真的不明白问题在哪里。 这里是一个播放器: @RunWith(VertxUnitRunner::class) class HelloWorldTest { private val vertx: Vertx = Vertx.vertx() @Before fun setUp(context: TestContext) { // HelloWorldVerticle is a simple http server that replies "Hello, World!" to whatever call vertx.deployVerticle(HelloWorldVerticle::class.java!!.getName(), context.asyncAssertSuccess()) } // ORIGINAL ASYNC TEST HERE. IT WORKS AS EXPECTED @Test fun testAsync(context: TestContext) { val atc = context.async() vertx.createHttpClient().getNow(8080, […]

Kotlin:ArrayIndexOutOfBoundsException在将DispatchTask转换为字符串期间

我们已经发布了我们的第一个Kotlin作品,我们遇到了这个stacktrace的崩溃: Fatal Exception: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 at kotlin.jvm.internal.ReflectionFactory.renderLambdaToString(ReflectionFactory.java:47) at kotlin.jvm.internal.Reflection.renderLambdaToString(Reflection.java:80) at kotlin.jvm.internal.Lambda.toString(Lambda.kt:22) at java.lang.StringBuilder.append(StringBuilder.java:202) at kotlinx.coroutines.experimental.DispatchedContinuation.toString(CoroutineDispatcher.kt:192) at java.lang.StringBuilder.append(StringBuilder.java:202) at kotlinx.coroutines.experimental.DispatchTask.toString(CoroutineDispatcher.kt:124) at java.lang.StringBuilder.append(StringBuilder.java:202) at android.os.Looper.loop(Looper.java:160) at android.app.ActivityThread.main(ActivityThread.java:5637) at java.lang.reflect.Method.invoke(Method.java) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 谷歌向我们展示了Android5上的这种情况。Crashlytics / Fabric也显示了Android 4.4的两个崩溃。 没有更新的Android版本崩溃。 我不知道如何防止这个崩溃或如何复制它。 当前版本: org.jetbrains.kotlinx:kotlinx协同程序核心:0.16

Spring 5和Kotlin 1.1协程:类型rx.Scheduler不存在

我正在使用Kotlin 1.1.4-3和Spring-context 5.0.0.RELEASE。 在启动项目,我得到这个错误: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'coroutineAnnotationBeanPostProcessor' defined in class path resource [org/springframework/kotlin/experimental/coroutine/context/ProxyCoroutineConfiguration.class]: Unsatisfied dependency expressed through method 'coroutineAnnotationBeanPostProcessor' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'globalCoroutineContextResolver' defined in class path resource [org/springframework/kotlin/experimental/coroutine/context/CoroutineContextResolverConfiguration.class]: Unexpected exception during bean creation; nested exception is java.lang.reflect.InvocationTargetException at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:723) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:458) at […]

在Android服务中的Kotlin协程

我有一个android服务,它启动并同步服务器上的不同类型的数据,当它在线。 我是Kotlin协程新手,我试图完成以下任务: fun syncData{ //Job1 make retrofit call to server //Job2 make retrofit call to server after job1 is done. //Job3 make retrofit call to server after job 2 is done and so on. //After all jobs are done I'll stop service. } 我正在关注这篇文章: Kotlin Coroutines是Android的正确方法 这给我带来了这个解决方案 fun syncData() = async(CommonPool){ try{ val sync1 = […]

如何在Java中轻松使用Kotlin渠道生产者?

作为开发新API的一部分,我正在学习使用Kotlin。 最初我希望Kotlin API能在Java(Android)项目中使用,但从长远来看,我希望完全采用Kotlin。 作为改进长期运行过程的一部分,我想使用协程。 具体来说, kotlinx.courtines包中的渠道生产者 。 例如: fun exampleProducer() = produce { send("Hello") delay(1000) send("World") } 在Java中使用这个最好的方法是什么? 我可以为Kotlin和/或Java添加临时的“帮手”功能。

kotlin中的“协同本地”变量

Java具有ThreadLocal变量,对于运行并行操作而言,不需要videoCapture.retrieve(image)其他线程或每循环分配,例如OpenCV使用videoCapture.retrieve(image) ,而“image”可以是threadlocal变量。 Kotlin是否具有“协同本地”变量的意义? 如果我想以他们的反例为例,但是每个协程都有一个计数器,那我该怎么做呢? for (i in 1..1_000_000) thread(start = true) { c.addAndGet(i) }

Kotlin过程集合并行?

我有一个对象的集合,我需要执行一些转换。 目前我正在使用: var myObjects: List<MyObject> = getMyObjects() myObjects.forEach{ myObj -> someMethod(myObj) } 它工作正常,但我希望通过并行运行someMethod()来加速它,而不是等待每个对象完成,然后再开始下一个对象。 Kotlin有没有办法做到这一点? 也许与doAsyncTask什么的? 我知道一年前这个问题是不可能的, 但是现在Kotlin拥有doAsyncTask这样的协同程序,我很好奇,如果任何协程都可以帮助的话