有什么方法可以在扑来中执行多线程

发布于 2025-02-10 13:18:20 字数 521 浏览 0 评论 0原文

文档说,即使在同一线程上执行异步任务,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缱倦旧时光 2025-02-17 13:18:20

您应该阅读飞镖文档。
https://dart.dart.dev/guides/guides/langueage/concurrency
DART支持并发编程

for(var image in images){
    processOnImage(image);    
}

procesOnImage(var image){
    var receive = ReceivePort();
    Isolate.spawn(yourFunctionToRun, receive.sendPort);
}

You should read the dart docs.
https://dart.dev/guides/language/concurrency
Dart supports concurrent programming

for(var image in images){
    processOnImage(image);    
}

procesOnImage(var image){
    var receive = ReceivePort();
    Isolate.spawn(yourFunctionToRun, receive.sendPort);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文