我正在用springboot和kotlin构建一个后端应用程序。我想实现一个特定方法的并行执行。我尝试使用协程,但底层方法是同步运行的。
这是我迄今为止所尝试的:
fun getXsByIds(xIds: List<String>): List<X> {
val xList = ArrayList<X>();
runBlocking {
val promises = xIds.map {
async {
getXById(it)
}
};
xList.addAll((promises.awaitAll()).filterNotNull())
}
return xList;
}
fun getXById(xId: String): X? {
// This method makes request to database
}
当我放入日志时
getXById
这样的方法
fun getXById(xId: String): X? {
print("start");
// This method makes request to database
print("end");
}
我得到这个输出
start
end
start
end
start
end
...
为什么我的郊游不并行?这是弹簧靴。没有反应。按请求线程样式。