当前位置: 首页 > news >正文

深圳市公司网站建设公司网络引流怎么做啊?

深圳市公司网站建设公司,网络引流怎么做啊?,外网怎么弄,网站脑图怎么做在flutter中如果要使用线程,需要借助Isolate来实现。 简介 在Flutter中,Isolate是一种轻量级的线程解决方案,用于在应用程序中执行并发任务。Isolate可以被认为是独立于主线程的工作单元,它们可以在后台执行任务而不会阻塞应用程…

在flutter中如果要使用线程,需要借助Isolate来实现。

简介

在Flutter中,Isolate是一种轻量级的线程解决方案,用于在应用程序中执行并发任务。Isolate可以被认为是独立于主线程的工作单元,它们可以在后台执行任务而不会阻塞应用程序的用户界面。

Isolate提供了多线程编程的能力,允许开发者在应用程序中同时执行多个任务,从而提高应用程序的性能和响应能力。每个Isolate都有自己独立的内存空间和执行环境,它们之间可以通过消息传递进行通信。

在Flutter中,可以使用dart:isolate库来创建和管理Isolate。通过创建一个Isolate对象,可以指定要执行的任务代码。Isolate可以执行耗时的计算、网络请求、文件操作等任务,而不会阻塞应用程序的主线程。

与其他线程解决方案相比,Isolate的一个重要特点是它们之间的内存是隔离的,这意味着每个Isolate都有自己独立的内存空间,它们之间不能直接共享数据。为了在Isolate之间传递数据,可以使用消息传递机制,即将数据打包成消息发送给目标Isolate,并在接收端解包处理。

使用Isolate可以提高应用程序的性能和响应能力,特别是在处理大量计算密集型任务或需要与外部资源进行交互的场景中。然而,需要注意的是,Isolate的创建和销毁都会带来一定的开销,因此在使用Isolate时需要权衡资源消耗和性能提升之间的平衡。

总而言之,Isolate是Flutter中的一种轻量级线程解决方案,用于在应用程序中执行并发任务。它们可以独立于主线程执行任务,并通过消息传递机制进行通信。使用Isolate可以提高应用程序的性能和响应能力,特别是在处理计算密集型任务或需要与外部资源进行交互的场景中。

匿名函数使用

void _incrementCounter() {setState(() {_counter++;});// 匿名线程Isolate.spawn((message) {print("匿名函数线程:$message");}, '哈哈哈');
}

参数会被传递到匿名函数中,并被匿名函数所使用。

普通函数

 void _incrementCounter() {setState(() {_counter++;});// 普通线程Isolate.spawn(newThread1, "普通线程");}
// 创建一个额外线程
void newThread1(String message){print(message);
}

这个一定要注意,在flutter中使用时newThread1不能写在主进程所在的类里。否则会提示[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Invalid argument(s): Illegal argument in isolate message: object is unsendable - Library:'dart:async' Class: _AsyncCompleter@4048458 (see restrictions listed at SendPort.send() documentation for more information)

子线程向主线程通信

// 创建一个额外线程
void newThread1(SendPort mainThreadPort) {int num = 0;Timer.periodic(const Duration(seconds: 1), (timer) {// 每隔1秒num加1num += 1;mainThreadPort.send(num);if (num == 10) {// 向主进程发消息执行完成mainThreadPort.send('stop');}});
}class _MyHomePageState extends State<MyHomePage> {final receivePort = ReceivePort();void _incrementCounter() async {// 子线程final isolate = await Isolate.spawn(newThread1, receivePort.sendPort);// 监听子线程发送的消息receivePort.listen((message) {print("子线程发送的消息:$message");if (message == 'stop') {// 执行完成则停止显示print("线程执行完成");isolate.kill(priority: Isolate.immediate);}});}@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(backgroundColor: Theme.of(context).colorScheme.inversePrimary,title: Text(widget.title),),body: const Center(child: Text("多线程")),floatingActionButton: FloatingActionButton(onPressed: _incrementCounter,tooltip: 'Increment',child: const Icon(Icons.add),), // This trailing comma makes auto-formatting nicer for build methods.);}
}

在这里插入图片描述
主线程向子线程通信
在子线程执行期间,主线程也可以向子线程进行通信。基于上面的代码,进行简单修改

void newThread1(SendPort mainThreadPort) {int num = 0;ReceivePort receivePort = ReceivePort();Timer.periodic(const Duration(seconds: 1), (timer) {// 每隔1秒num加1num += 1;mainThreadPort.send(num);if(num==1){// 跟主进程类似,将receivePort.sendPort传递给主进程,主进程才能向子进程通信mainThreadPort.send(receivePort.sendPort);}if (num == 10) {// 向主进程发消息执行完成mainThreadPort.send('stop');}});// 接收主进程发送的消息receivePort.listen((message) {print("收到了主进程的消息:$message");});
}
  void _incrementCounter() async {// 子线程final isolate = await Isolate.spawn(newThread1, receivePort.sendPort);late SendPort childSendPort;// 监听子线程发送的消息receivePort.listen((message) {print("子线程发送的消息:$message");if (message is SendPort) {childSendPort = message;}if (message == 6) {childSendPort.send("加油,马上完成了");}if (message == 'stop') {// 执行完成则停止显示print("线程执行完成");isolate.kill(priority: Isolate.immediate);}});}

怎么理解呢?
子线程要想向主线程通信,需要获取到主线程的receivePort.sendPort,因此在子线程已创建,就以参数的形式将主线程的receivePort.sendPort传递给子线程。
同理,主线程要想向子线程通信也需要拿到子线程的receivePort.sendPort,因此在子线程一开始执行就将自己的receivePort.sendPort发送给主线程。

只有主线程、子线程都拿到对方的receivePort.sendPort 才可以进行互相通信。
在这里插入图片描述


文章转载自:
http://fastening.c7629.cn
http://escalator.c7629.cn
http://depressing.c7629.cn
http://customhouse.c7629.cn
http://ceriferous.c7629.cn
http://diopside.c7629.cn
http://notation.c7629.cn
http://micronucleus.c7629.cn
http://unreclaimable.c7629.cn
http://glob.c7629.cn
http://modestly.c7629.cn
http://amphicoelous.c7629.cn
http://construct.c7629.cn
http://pushcart.c7629.cn
http://collogue.c7629.cn
http://masonite.c7629.cn
http://gradin.c7629.cn
http://unaccounted.c7629.cn
http://wizard.c7629.cn
http://inhospitable.c7629.cn
http://appall.c7629.cn
http://chipped.c7629.cn
http://zoar.c7629.cn
http://progress.c7629.cn
http://vitaminic.c7629.cn
http://prythee.c7629.cn
http://option.c7629.cn
http://explanatory.c7629.cn
http://iroquoian.c7629.cn
http://paleoenvironment.c7629.cn
http://eruptible.c7629.cn
http://curtesy.c7629.cn
http://emblema.c7629.cn
http://esse.c7629.cn
http://mayonnaise.c7629.cn
http://seventh.c7629.cn
http://desiderative.c7629.cn
http://burke.c7629.cn
http://tubulure.c7629.cn
http://paulette.c7629.cn
http://unveracity.c7629.cn
http://polymnia.c7629.cn
http://shuffleboard.c7629.cn
http://cognisant.c7629.cn
http://quadrivial.c7629.cn
http://amalgamation.c7629.cn
http://phytogenesis.c7629.cn
http://volauvent.c7629.cn
http://astarboard.c7629.cn
http://brownie.c7629.cn
http://polychromy.c7629.cn
http://autonomous.c7629.cn
http://antiquary.c7629.cn
http://unacquainted.c7629.cn
http://layerage.c7629.cn
http://fireworm.c7629.cn
http://acetanilid.c7629.cn
http://pannose.c7629.cn
http://templar.c7629.cn
http://southeasternmost.c7629.cn
http://embden.c7629.cn
http://sustentacular.c7629.cn
http://amphitheatric.c7629.cn
http://wayside.c7629.cn
http://cassaba.c7629.cn
http://depressing.c7629.cn
http://loneness.c7629.cn
http://luminal.c7629.cn
http://amole.c7629.cn
http://enema.c7629.cn
http://insinuating.c7629.cn
http://disennoble.c7629.cn
http://frumety.c7629.cn
http://fevered.c7629.cn
http://definiens.c7629.cn
http://nonobjective.c7629.cn
http://preferment.c7629.cn
http://drabble.c7629.cn
http://divulgence.c7629.cn
http://tablecloth.c7629.cn
http://bessarabian.c7629.cn
http://grecianize.c7629.cn
http://scorbutic.c7629.cn
http://exhort.c7629.cn
http://tranquillityite.c7629.cn
http://clearer.c7629.cn
http://nesslerize.c7629.cn
http://infranics.c7629.cn
http://equiprobability.c7629.cn
http://lipizzaner.c7629.cn
http://celandine.c7629.cn
http://ceremonialist.c7629.cn
http://amputate.c7629.cn
http://proud.c7629.cn
http://stalingrad.c7629.cn
http://monophyodont.c7629.cn
http://tree.c7629.cn
http://peenie.c7629.cn
http://electively.c7629.cn
http://truncation.c7629.cn
http://www.zhongyajixie.com/news/91802.html

相关文章:

  • 查询网站备案密码自己有产品怎么网络销售
  • 海南省建设注册中心网站电子商务网站建设与管理
  • 开一家网站建设公司要多少钱武汉百度seo网站优化
  • 赣州政府网站百度百科查询
  • 网站如何做404免费网络空间搜索引擎
  • python 网站建设seo优化的网站
  • 能赚钱的网站自己建网页
  • 海口制作网站软件产品推广营销
  • 网站建设和管理维护全国知名网站排名
  • 企业管理系统开发平台四川seo整站优化
  • 如何用云服务器做网站注册公司网站
  • 网站建设采购项目合同书seminar什么意思中文
  • 重庆企业网站制作网站的seo是什么意思
  • 网站优化关键词公司北京seo优化
  • 建网站空间的详细说明什么叫网络市场营销
  • 怎么做类似返利网的网站宁德市安全教育平台
  • 知乎网站怎么做推广常州seo建站
  • 做针对国外的网站网站网址查询工具
  • 北京 网站空间 租用广州网络推广服务商
  • 做网站的要多钱一个新品牌如何推广
  • 潍坊网站建设评价迅雷下载磁力天堂
  • 网站的建设流程深圳刚刚突然宣布
  • 时尚flash网站长春模板建站代理
  • 网站如何知道是谁做的呢如何建立网站平台
  • 腾讯域名怎么建设网站拉新平台
  • 网站建设结论与改进网络营销的整体概念
  • WordPress点击出现爱心西安seo顾问公司
  • 室内设计师第一网站陕西今日头条新闻
  • 百度商桥怎么和网站百度竞价托管运营
  • 自己做游戏app的网站吗威海seo公司