简体   繁体   English

有没有办法在颤振中执行多线程

[英]Is there any way to perform multithreading in flutter

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.文档说即使异步任务在同一个线程上执行,flutter 也是单线程的,这意味着当我们等待另一个任务完成时,我们将继续执行我们的同步代码。 But I have a situation where i need to process on multiple images, and process on each image takes approx 1.5 secconds.但是我有一种情况,我需要处理多个图像,并且处理每个图像大约需要 1.5 秒。 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.因此,在上面的脚本中,如果我有 20 张图像,则大约需要 30 秒,这在我的情况下太高了。 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 
}

You should read the dart docs.你应该阅读飞镖文档。 https://dart.dev/guides/language/concurrency Dart supports concurrent programming https://dart.dev/guides/language/concurrency Dart 支持并发编程

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM