TornadoFx FXTask OutOfMemoryError
TornadoFx FXTask OutOfMemoryError
所以我有一段有趣的代码,我 运行 进入了 OutOfMemoryError。
所以我的问题是我在 searchThread 中创建了新线程,这些线程正在再次搜索。这显然会创建一个 OutOfMemoryError,但我只想使用 TornadoFX 代码来解决这个问题,但没有任何运气。
searchThread = runAsync {
while (!searchThread.isCancelled) {
runAsync {
// Searching for Sth
} ui {
// Updating UI
}
}
}
}
如果我的搜索线程中的 运行Async 仍处于 运行ning 状态,我如何才能跳过新线程的创建?
你正在做的是在一个紧密的循环中创建新任务,所以很明显你会 运行 内存不足。嵌套runAsync
的调用不会等待,直到条件为假再执行一次。
删除内部 runAsync
并做任何您想做的事情,如果您想在 UI 线程上更新某些内容,请调用 runLater
。
我想我明白你的问题了。您的目标是只有一个搜索线程在已经 运行 时不被调用。正如 Edvin 所说,循环调用异步线程真的很糟糕。更不用说,嵌套线程甚至可能没有终止条件。这将是一个简单的解决方案,但这不是更有意义吗?:
val searchTask: Task<YourReturnType>? = null
private fun search() {
if(searchTask?.isRunning != true) {
searchTask = runAsync {
//Do your search thread things
} ui { result ->
//do things with your UI based on your result
}
}
}
同样,如果您想用新的 运行 搜索线程替换旧的搜索线程,您可以尝试类似的操作:
val searchTask: Task<YourReturnType>? = null
private fun search() {
if(searchTask?.isRunning == true) {
searchTask?.cancel()
//You should probably do something to check if the cancel succeeded.
}
searchTask = runAsync {
//Do your search thread things
} ui { result ->
//do things with your UI based on your result
}
}
所以我有一段有趣的代码,我 运行 进入了 OutOfMemoryError。
所以我的问题是我在 searchThread 中创建了新线程,这些线程正在再次搜索。这显然会创建一个 OutOfMemoryError,但我只想使用 TornadoFX 代码来解决这个问题,但没有任何运气。
searchThread = runAsync {
while (!searchThread.isCancelled) {
runAsync {
// Searching for Sth
} ui {
// Updating UI
}
}
}
}
如果我的搜索线程中的 运行Async 仍处于 运行ning 状态,我如何才能跳过新线程的创建?
你正在做的是在一个紧密的循环中创建新任务,所以很明显你会 运行 内存不足。嵌套runAsync
的调用不会等待,直到条件为假再执行一次。
删除内部 runAsync
并做任何您想做的事情,如果您想在 UI 线程上更新某些内容,请调用 runLater
。
我想我明白你的问题了。您的目标是只有一个搜索线程在已经 运行 时不被调用。正如 Edvin 所说,循环调用异步线程真的很糟糕。更不用说,嵌套线程甚至可能没有终止条件。这将是一个简单的解决方案,但这不是更有意义吗?:
val searchTask: Task<YourReturnType>? = null
private fun search() {
if(searchTask?.isRunning != true) {
searchTask = runAsync {
//Do your search thread things
} ui { result ->
//do things with your UI based on your result
}
}
}
同样,如果您想用新的 运行 搜索线程替换旧的搜索线程,您可以尝试类似的操作:
val searchTask: Task<YourReturnType>? = null
private fun search() {
if(searchTask?.isRunning == true) {
searchTask?.cancel()
//You should probably do something to check if the cancel succeeded.
}
searchTask = runAsync {
//Do your search thread things
} ui { result ->
//do things with your UI based on your result
}
}