有什么方法可以在扑来中执行多线程
文档说,即使在同一线程上执行异步任务,flutter是单线线程,这意味着当我们等待完成另一个任务完成时,我们将继续执行同步代码。但是我有一个情况,我需要在多个图像上进行处理,并且每个图像上的过程大约需要1.5个份额。示例代码以下是
for(var image in images){
processOnImage(image);
}
processOnImage(var image){
// it takes approx 1.5 secconds
}
在上面的脚本中,如果我有20张图像,则大约需要30个sccond,在我的情况下,这太高了。 所以我想知道有什么方法可以开始多线程以减少时间消耗,如下
for(var image in images){
startNewThread processOnImage(image);
}
processOnImage(var image){
// it takes approx 1.5 secconds
}
Documentation says flutter is single threaded even the asynchronous task is executed on the same thread which means that while we wait for another task to be completed, we will continue executing our synchronous code. But I have a situation where i need to process on multiple images, and process on each image takes approx 1.5 secconds. Sample code for this is below
for(var image in images){
processOnImage(image);
}
processOnImage(var image){
// it takes approx 1.5 secconds
}
So in above script if i have 20 images then it takes approx 30 seccond, which is too high in my case.
So I want to know that is there any way to start multithreading to reduce time consumption, like below
for(var image in images){
startNewThread processOnImage(image);
}
processOnImage(var image){
// it takes approx 1.5 secconds
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该阅读飞镖文档。
https://dart.dart.dev/guides/guides/langueage/concurrency
DART支持并发编程
You should read the dart docs.
https://dart.dev/guides/language/concurrency
Dart supports concurrent programming