Flutter应用线程
困惑于flutter不同的线程模型,探究一下isolate, runner, thread(system level)之间的关系。
ps -T -p [pid]
以本地的一个flutter app为例
u0_a361 17816 17816 522 1432184 185848 0 0 S m.example.test1
u0_a361 17816 17825 522 1432184 185848 0 0 S Jit thread pool //dart debug mode
u0_a361 17816 17830 522 1432184 185848 0 0 S Signal Catcher //处理linux signal通信(SIGQUIT,SIGUSR1等),打印进程堆栈,生成trace.txt
u0_a361 17816 17831 522 1432184 185848 0 0 S ADB-JDWP Connec //adb debug connection
u0_a361 17816 17832 522 1432184 185848 0 0 S HeapTaskDaemon //java 堆gc线程
u0_a361 17816 17833 522 1432184 185848 0 0 S ReferenceQueueD //引用队列的守护线程
u0_a361 17816 17834 522 1432184 185848 0 0 S FinalizerDaemon //析构的守护线程
u0_a361 17816 17835 522 1432184 185848 0 0 S FinalizerWatchd //析构监控的守护线程
u0_a361 17816 17836 522 1432184 185848 0 0 S Binder:17816_1 //bind通信线程
u0_a361 17816 17837 522 1432184 185848 0 0 S Binder:17816_2 //bind通信线程
u0_a361 17816 17839 522 1432184 185848 0 0 S Binder:17816_3 //bind通信线程
u0_a361 17816 17852 522 1432184 185848 0 0 S Profile Saver //debug时的分析线程
u0_a361 17816 17857 522 1432184 185848 0 0 S RenderThread //render线程
u0_a361 17816 17862 522 1432184 185848 0 0 S Binder:intercep //Binder通信
u0_a361 17816 17865 522 1432184 185848 0 0 S AsyncTask #1 //每个应用都有的AsyncTask
u0_a361 17816 17877 522 1432184 185848 0 0 S 1.ui //UI Runner
u0_a361 17816 17878 522 1432184 185848 0 0 S 1.raster //GPU Runner
u0_a361 17816 17879 522 1432184 185848 0 0 S 1.io //IO Runner
u0_a361 17816 17880 522 1432184 185848 0 0 S m.example.test1
u0_a361 17816 17881 522 1432184 185848 0 0 S m.example.test1
u0_a361 17816 17882 522 1432184 185848 0 0 S m.example.test1
u0_a361 17816 17883 522 1432184 185848 0 0 S m.example.test1
u0_a361 17816 17884 522 1432184 185848 0 0 S m.example.test1
u0_a361 17816 17885 522 1432184 185848 0 0 S m.example.test1
u0_a361 17816 17886 522 1432184 185848 0 0 S m.example.test1
u0_a361 17816 17887 522 1432184 185848 0 0 S m.example.test1
u0_a361 17816 17888 522 1432184 185848 0 0 S m.example.test1
u0_a361 17816 17889 522 1432184 185848 0 0 S Dart Profiler T //dart 分析器
u0_a361 17816 17890 522 1432184 185848 0 0 S DartWorker
u0_a361 17816 17894 522 1432184 185848 0 0 S ConnectivityThr
u0_a361 17816 17899 522 1432184 185848 0 0 S DartWorker
u0_a361 17816 17901 522 1432184 185848 0 0 S hwuiTask0 //结合renderTread一起控制渲染
u0_a361 17816 17902 522 1432184 185848 0 0 S hwuiTask1 //结合renderTread一起控制渲染
未标注的线程没有找到相关资料
根据
可知 Isolate其实是对特定平台的线程封装,不共享变量,仅通过channel进行通信,避免数据竞争。
Isolate中会有「MicroTask」和 「Event」,「MicroTask」优先级高于[Event], 优先执行microtask中的任务,microtask中没有任务时才执行event中的任务。
Future执行有两种方式:
Future.microtask 加入「MicroTask」
Future加入event
这如果没有在单独的Isolate中使用Future,这个Future将在当前main Isolate中执行,ui刷新等执行都是在Event中,所以如果Future的任务耗时较长需要在其他Isolate中执行。
Runner有以下几个:
PlatformRunner,处理与Engine交互他自平台的消息,与跟Flutter Engine的所有交互(接口调用)必须发生在Platform Thread。
IO Runner, 主要功能是从图片存储(比如磁盘)中读取压缩的图片格式,将图片数据进行处理为GPU Runner的渲染做好准备
UI Runner, 执行dart代码,即通过执行dart root isolate 执行main.dart下的内容,当收到vsync信号时,通过dart root isolate获取当前需要需要渲染的内容进行处理。处理完成后提交给GPU Runner后续处理
GPU Runner,运行在cpu的线程,进一步处理后将帧提交给gpu显示。
这4个runner是由flutter engin提供的处理特定事务的线程。 dart code无法直接访问这4个线程,因为dart code运行在dartVM里的。
Runner是flutter engin提供的处理flutter自身业务的线程,外部无法直接访问。
Isolate是由dartVM提供的线程模型,是对操作系统线程的封装。
Future可以用来在当前Isolate处理一些毫秒级任务,太耗时的任务(IO,Compute)需要单独开Isolate进行操作。