java - 如何通过 JNI 从同步 Java 函数调用异步 rust 函数?
问题描述
我目前正在尝试使用 Rust JNI crate 在 Java 中运行异步函数。我想知道是否有办法foo.async
在同步功能下运行异步任务。这是在 Java 中运行异步。所以基本上我需要一个fn
来运行一个async fn
.
package quest.evo.runtime
import quest.evo.runtime.objects.MainWorker
import java.io.File
import java.io.FileOutputStream
object Library {
external fun create_main_worker(): MainWorker;
init {
val tempDir = File(System.getProperty("java.io.tmpdir"))
try {
if (tempDir.exists()) {
val os = System.getProperty("os.name")
val extension = if (os.startsWith("Windows")) ".dll" else if (os.startsWith("Mac OS")) ".dylib" else ".so"
if (tempDir.canWrite()) {
val file = File(tempDir, "libevo$extension")
file.delete()
file.createNewFile()
try {
Library::class.java.classLoader.getResourceAsStream("libevo$extension").use { inputStream ->
FileOutputStream(file.absolutePath).use { outputStream ->
val buffer = ByteArray(4096)
if (inputStream != null) {
while (true) {
val length = inputStream.read(buffer)
if (length == -1) {
break
}
outputStream.write(buffer, 0, length)
}
}
}
}
} catch (ignored: Exception) {
}
System.load(file.absolutePath)
} else {
println("Cannot write to the temp directory.")
}
} else {
println("Cannot find the temp directory.")
}
} catch (e: Exception) {
e.printStackTrace()
}
}
@JvmStatic
fun main(args: Array<String>) {
val test = create_main_worker();
}
}
解决方案
推荐阅读
- python - 如何在 python 中处理单个 socket.io 客户端事件?
- javascript - 如何使用 typescrpt 在外部 js 文件中调用函数
- r - 具有指定格式参数的 R strptime 函数恢复为默认值
- python - 当它们位于不同的熊猫列中时,如何绘制小时和分钟?
- amazon-web-services - 如何将 Amazon ECS 添加到我已经创建的非默认 VPC?
- .net - 在 VB.NET 中读取 .csv 文件的单元格中的特定值
- python - 在 Python 中添加计算列以计算列的频率
- python - 如何在极坐标 matplotlib 图上绘制带有文本(即标签)的水平线?(Python)
- android - 实时数据库 Firebase 规则 - 拒绝权限
- outlook - Outlook 插件无法在 Office 2013 或 Office 2016 上运行