我可以利用Kotlin';通过在Java代码中使用它们来实现协同路由?
我的目标是什么? 我的目标是能够从Java使用Kotlin的协同程序系统。我希望能够在给定的时间内暂停执行中,然后在给定的时间过后在该点恢复。在Java中,我希望能够以异步方式执行允许暂停中间执行的任务,例如:我可以利用Kotlin';通过在Java代码中使用它们来实现协同路由?,java,kotlin,coroutine,Java,Kotlin,Coroutine,我的目标是什么? 我的目标是能够从Java使用Kotlin的协同程序系统。我希望能够在给定的时间内暂停执行中,然后在给定的时间过后在该点恢复。在Java中,我希望能够以异步方式执行允许暂停中间执行的任务,例如: //example 1 someLogic(); pause(3000L); //3 seconds someMoreLogic(); //example 2 while(true) { someContinuedLogic(); pause(10000L); //10
//example 1
someLogic();
pause(3000L); //3 seconds
someMoreLogic();
//example 2
while(true) {
someContinuedLogic();
pause(10000L); //10 seconds
}
我的问题是什么?
正如预期的那样,我能够从Kotlin中完美地执行协同路由,但是当涉及到Java时,它变得很棘手,因为代码的Java部分一次执行整个块,没有任何暂停,而Kotlin块正确地暂停1秒,然后是4秒
我的问题是什么?
甚至可以使用Kotlin作为Java中协同路由的主干吗?如果是,我做错了什么?下面您可以找到显示我如何尝试在Java中使用Kotlin的协程的源代码
KtScript类
abstract class KtScript {
abstract fun execute()
fun <T> async(block: suspend () -> T): CompletableFuture<T> {
val future = CompletableFuture<T>()
block.startCoroutine(completion = object : Continuation<T> {
override fun resume(value: T) {
future.complete(value)
}
override fun resumeWithException(exception: Throwable) {
future.completeExceptionally(exception)
}
})
return future
}
suspend fun <T> await(f: CompletableFuture<T>): T =
suspendCoroutine { c: Continuation<T> ->
f.whenComplete { result, exception ->
if (exception == null)
c.resume(result)
else
c.resumeWithException(exception)
}
}
fun pause(ms: Long): CompletableFuture<*> {
//todo - a better pausing system (this is just temporary!)
return CompletableFuture.runAsync {
val currentMs = System.currentTimeMillis()
while (System.currentTimeMillis() - currentMs < ms) {
/* do nothing */
}
}
}
}
Executing Kotlin script from Kotlin...
1 second passed...
5 seconds passed...
Finished!
Executing Kotlin script from Java...
1 second passed...
5 seconds passed...
Finished!
Java执行代码
fun main(args: Array<String>) {
ScriptTestKotlin().execute()
}
class ScriptTestKotlin : KtScript() {
override fun execute() {
println("Executing Kotlin script from Kotlin...")
val future = async {
await(pause(1000L))
println(" 1 second passed...")
await(pause(4000L))
println(" 5 seconds passed...")
}
future.get() //wait for asynchronous task to finish
println("Finished!")
}
}
public class ScriptTestJava extends KtScript {
public static void main(String[] args) {
new ScriptTestJava().execute();
}
@Override
public void execute() {
System.out.println("Executing Kotlin script from Java...");
CompletableFuture<?> future = async(continuation -> {
await(pause(1000L), continuation);
System.out.println(" 1 second passed...");
await(pause(4000L), continuation);
System.out.println(" 5 seconds passed...");
return continuation;
});
try {
future.get(); //wait for asynchronous task to finish
} catch(Exception e) {
e.printStackTrace();
}
System.out.println("Finished!");
}
}
^^^不幸的是,Java中跳过了暂停^^^ Kotlin协程是通过对代码的编译器转换来实现的,这显然只能由
kotlinc
来完成
因此,不,Java不能使用Kotlin的协程机制,因为它是编译时特性 为什么不直接切换到Kotlin呢?如果您的库是使用协同程序用Kotlin编写的,并且作为JAR包含在Java项目中,该怎么办。Java能够利用Kotlin编写的调用方法来利用协同路由吗?从Java POV来看,这样的库看起来就像普通的字节码,带有回调和对kotlinx.coroutines库类的一些方法调用。所以和其他异步技术没有什么不同