当前位置:网站首页>Flutter3.0线程——四步教你如何全方位了解(事件队列)
Flutter3.0线程——四步教你如何全方位了解(事件队列)
2022-08-04 01:39:00 【守住Android最后的光】
一、应用场景
- 队列压缩视频
- 队列解密视频
- 队列请求网络
- 等等
二、实现思路
- 定义一个任务队列taskList [先进先出]
- 提供添加任务方法
- 取第一个任务执行
- 执行完后,从taskList移除
- 递归获取第一个任务并执行任务
- 直到taskList为空停止队列任务
三、具体实现
class QueueUtil {
/// 用map key存储多个QueueUtil单例,目的是隔离多个类型队列任务互不干扰
/// Use map key to store multiple QueueUtil singletons, the purpose is to isolate multiple types of queue tasks without interfering with each other
static Map<String, QueueUtil> _instance = Map<String, QueueUtil>();
static QueueUtil get(String key) {
if (_instance[key] == null) {
_instance[key] = QueueUtil._();
}
return _instance[key];
}
QueueUtil._() {
/// 初始化代码
}
List<_TaskInfo> _taskList = [];
bool _isTaskRunning = false;
int _mId = 0;
bool _isCancelQueue = false;
Future<_TaskInfo> addTask(Function doSomething) {
_isCancelQueue = false;
_mId++;
_TaskInfo taskInfo = _TaskInfo(_mId, doSomething);
/// 创建future
Completer<_TaskInfo> taskCompleter = Completer<_TaskInfo>();
/// 创建当前任务stream
StreamController<_TaskInfo> streamController = new StreamController();
taskInfo.controller = streamController;
/// 添加到任务队列
_taskList.add(taskInfo);
/// 当前任务的stream添加监听
streamController.stream.listen((_TaskInfo completeTaskInfo) {
if (completeTaskInfo.id == taskInfo.id) {
taskCompleter.complete(completeTaskInfo);
streamController.close();
}
});
/// 触发任务
_doTask();
return taskCompleter.future;
}
void cancelTask() {
_taskList = [];
_isCancelQueue = true;
_mId = 0;
_isTaskRunning = false;
}
_doTask() async {
if (_isTaskRunning) return;
if (_taskList.isEmpty) return;
/// 取任务
_TaskInfo taskInfo = _taskList[0];
_isTaskRunning = true;
/// 模拟执行任务
await taskInfo.doSomething?.call();
taskInfo.controller.sink.add(taskInfo);
if (_isCancelQueue) return;
/// 出队列
_taskList.removeAt(0);
_isTaskRunning = false;
/// 递归执行任务
_doTask();
}
}
class _TaskInfo {
int id; // 任务唯一标识
Function doSomething;
StreamController<_TaskInfo> controller;
_TaskInfo(this.id, this.doSomething, {this.controller});
}
四、使用
main() {
/// 将任务添加到队列
print("加入队列-net, taskNo: 1");
QueueUtil.get("net").addTask(() {
return _doFuture("net", 1);
});
print("加入队列-net, taskNo: 2");
QueueUtil.get("net").addTask(() {
return _doFuture("net", 2);
});
print("加入队列-local, taskNo: 1");
QueueUtil.get("local").addTask(() {
return _doFuture("local", 1);
});
/// 取消队列任务
/// QueueUtil.get("net").cancelTask();
}
Future _doFuture(String queueName, int taskNo) {
return Future.delayed(Duration(seconds: 2), () {
print("任务完成 queueName: $queueName, taskNo: $taskNo");
});
}
// 执行结果:
I/flutter (26436): 加入队列-net, taskNo: 1
I/flutter (26436): 加入队列-net, taskNo: 2
I/flutter (26436): 加入队列-local, taskNo: 1
------------两秒后--------
I/flutter (26436): 任务完成 queueName: net, taskNo: 1
I/flutter (26436): 任务完成 queueName: local, taskNo: 1
------------两秒后--------
I/flutter (26436): 任务完成 queueName: net, taskNo: 2
上述就是flutter的线程中的;事件队列。我们从应用场景、实现思路、具体实现、到使用、四个方面了解并使用。flutter 知识学习除了文章中flutter线程事件队列一小部分还分别区分了这几大块:Dart语法基础+语法进阶+flutter UI+flutter线程+flutter启动流程+flutter之framework框架+flutter监控学习。
如果你正好在学习flutter,或者准备学习flutter。在学习中遇到不知道该怎么学习。或者不知从哪开始;可以看我总结如下图的学习指南图:![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IhzYQWwb-1659536709732)(https://upload-images.jianshu.io/upload_images/28055132-6770185fa5013cf1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)]](/img/fd/6de71b9b74203ce3da88271a78c372.png)
针对flutter,总结出的核心技术学习方法。整理出一套相对应的资料配合学习。
可以看出图中学习思路很清晰,配合资料学习起来比网上杂乱的资料,学习来事半功倍;如有急切想进阶自己技术,跳槽找工作的。可以上面获取,免费,还望多给博主一些点赞+关注;可以多评论互动哟!
边栏推荐
- 2022 China Computing Power Conference released the excellent results of "Innovation Pioneer"
- 网页三维虚拟展厅为接入元宇宙平台做基础
- Summary of GNSS Articles
- 【Untitled】
- 快速入门EasyX图形编程
- 谁说程序员不懂浪漫,表白代码来啦~
- Installation and configuration of nodejs+npm
- Vant3 - click on the corresponding name name to jump to the next page corresponding to the location of the name of the TAB bar
- 即席查询——Presto
- 计算首屏时间
猜你喜欢
通用的测试用例编写大全(登录测试/web测试等)
一个项目的整体测试流程有哪几个阶段?测试方法有哪些?

esp32 releases robot battery voltage to ros2 (micro-ros+CoCube)

循环绕过问题

Deng Qinglin, Alibaba Cloud Technical Expert: Best Practices for Disaster Recovery across Availability Zones and Multiple Lives in Different Locations on the Cloud

工程制图复习题(带答案)

敏捷交付的工程效能治理

jmeter distributed stress test

SAP SD模块前台操作

字符串的排列
随机推荐
nodejs install multi-version version switching
【正则表达式】笔记
Observability:你所需要知道的关于 Syslog 的一些知识
如何用C语言代码实现商品管理系统开发
114. How to find the cause of Fiori Launchpad routing error by single-step debugging
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
Slipper —— 虚点,最短路
敏捷交付的工程效能治理
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
【Untitled】
lombok注解@RequiredArgsConstructor的使用
5.scrapy中间件&分布式爬虫
静态文件快速建站
Download install and create/run project for HBuilderX
持续投入商品研发,叮咚买菜赢在了供应链投入上
GraphQL背后处理及执行过程是什么
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
Android interview questions and answer analysis of major factories in the first half of 2022 (continuously updated...)
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
《Greenplum构建实时数据仓库实践》简介