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

解决方案网站营销型的网站域名

解决方案网站,营销型的网站域名,小程序商店推荐,深圳英文网站设计HarmonyOS App(JAVA)多线程任务分发器 打印时间,记录到编辑框textfield信息显示 同步分发,异步分发,异步延迟分发,分组任务分发,屏蔽任务分发,多次任务分发 参考代码注释 场景介绍 如果应用的业务逻辑比…

HarmonyOS App(JAVA)多线程任务分发器

打印时间,记录到编辑框textfield信息显示

同步分发,异步分发,异步延迟分发,分组任务分发,屏蔽任务分发,多次任务分发

参考代码注释

场景介绍

如果应用的业务逻辑比较复杂,可能需要创建多个线程来执行多个任务。这种情况下,代码复杂难以维护,任务与线程的交互也会更加繁杂。要解决此问题,开发者可以使用“TaskDispatcher”来分发不同的任务。

接口说明

TaskDispatcher是一个任务分发器,它是Ability分发任务的基本接口,隐藏任务所在线程的实现细节。

为保证应用有更好的响应性,我们需要设计任务的优先级。在UI线程上运行的任务默认以高优先级运行,如果某个任务无需等待结果,则可以用低优先级。

表1 线程优先级介绍

优先级

详细描述

HIGH

最高任务优先级,比默认优先级、低优先级的任务有更高的几率得到执行。

DEFAULT

默认任务优先级, 比低优先级的任务有更高的几率得到执行。

LOW

低任务优先级,比高优先级、默认优先级的任务有更低的几率得到执行。

TaskDispatcher具有多种实现,每种实现对应不同的任务分发器。在分发任务时可以指定任务的优先级,由同一个任务分发器分发出的任务具有相同的优先级。系统提供的任务分发器有GlobalTaskDispatcher、ParallelTaskDispatcher、SerialTaskDispatcher 、SpecTaskDispatcher。

  • GlobalTaskDispatcher

    全局并发任务分发器,由Ability执行getGlobalTaskDispatcher()获取。适用于任务之间没有联系的情况。一个应用只有一个GlobalTaskDispatcher,它在程序结束时才被销毁。

     
    1. TaskDispatcher globalTaskDispatcher = getGlobalTaskDispatcher(TaskPriority.DEFAULT);
  • ParallelTaskDispatcher

    并发任务分发器,由Ability执行createParallelTaskDispatcher()创建并返回。与GlobalTaskDispatcher不同的是,ParallelTaskDispatcher不具有全局唯一性,可以创建多个。开发者在创建或销毁dispatcher时,需要持有对应的对象引用 。

     
    1. String dispatcherName = "parallelTaskDispatcher";
    2. TaskDispatcher parallelTaskDispatcher = createParallelTaskDispatcher(dispatcherName, TaskPriority.DEFAULT);
  • SerialTaskDispatcher

    串行任务分发器,由Ability执行createSerialTaskDispatcher()创建并返回。由该分发器分发的所有的任务都是按顺序执行,但是执行这些任务的线程并不是固定的。如果要执行并行任务,应使用ParallelTaskDispatcher或者GlobalTaskDispatcher,而不是创建多个SerialTaskDispatcher。如果任务之间没有依赖,应使用GlobalTaskDispatcher来实现。它的创建和销毁由开发者自己管理,开发者在使用期间需要持有该对象引用。

     
    1. String dispatcherName = "serialTaskDispatcher";
    2. TaskDispatcher serialTaskDispatcher = createSerialTaskDispatcher(dispatcherName, TaskPriority.DEFAULT);
  • SpecTaskDispatcher

    专有任务分发器,绑定到专有线程上的任务分发器。目前已有的专有线程为UI线程,通过UITaskDispatcher进行任务分发。

    UITaskDispatcher:绑定到应用主线程的专有任务分发器, 由Ability执行getUITaskDispatcher()创建并返回。 由该分发器分发的所有的任务都是在主线程上按顺序执行,它在应用程序结束时被销毁。

     
    1. TaskDispatcher uiTaskDispatcher = getUITaskDispatcher();
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:alignment="center"ohos:orientation="vertical"><Textohos:id="$+id:text_helloworld"ohos:height="match_content"ohos:width="match_content"ohos:background_element="$graphic:background_ability_main"ohos:layout_alignment="horizontal_center"ohos:text="$string:mainability_HelloWorld"ohos:text_size="40vp"/><Buttonohos:id="$+id:btn_tongbu"ohos:height="match_content"ohos:width="match_content"ohos:background_element="#EB0BF647"ohos:layout_alignment="horizontal_center"ohos:text="同步分发"ohos:text_size="40vp"/><Buttonohos:id="$+id:btn_yibu"ohos:height="match_content"ohos:width="match_content"ohos:background_element="#EB3D5DFC"ohos:layout_alignment="horizontal_center"ohos:text="异步分发"ohos:text_size="40vp"/><Buttonohos:id="$+id:btn_yibu_yanchi"ohos:height="match_content"ohos:width="match_content"ohos:background_element="#EB0BF647"ohos:layout_alignment="horizontal_center"ohos:text="异步延迟分发"ohos:text_size="40vp"/><Buttonohos:id="$+id:btn_fenzhu_task"ohos:height="match_content"ohos:width="match_content"ohos:background_element="#EB3D5DFC"ohos:layout_alignment="horizontal_center"ohos:text="分组任务分发"ohos:text_size="40vp"/><Buttonohos:id="$+id:btn_pingbi_task"ohos:height="match_content"ohos:width="match_content"ohos:background_element="#EB0BF647"ohos:layout_alignment="horizontal_center"ohos:text="屏蔽任务分发"ohos:text_size="40vp"/><Buttonohos:id="$+id:btn_duoci_task"ohos:height="match_content"ohos:width="match_content"ohos:background_element="#EB3D5DFC"ohos:layout_alignment="horizontal_center"ohos:text="多次任务分发"ohos:text_size="40vp"/><Textohos:id="$+id:text_label"ohos:height="match_content"ohos:width="match_content"ohos:background_element="$graphic:background_ability_main"ohos:layout_alignment="left"ohos:text="信息显示:"ohos:multiple_lines="true"ohos:text_size="20vp"/><TextFieldohos:id="$+id:text_filed_info"ohos:height="300vp"ohos:background_element="#FC0A84EF"ohos:text="信息显示区域"ohos:width="350vp"ohos:hint=""ohos:margin="2vp"ohos:text_size="20vp"/><Clockohos:id="$+id:clock"ohos:height="match_content"ohos:width="match_content"ohos:background_element="#FF80EF66"ohos:layout_alignment="left"ohos:text_size="20vp"/></DirectionalLayout>
package com.example.myapplication.slice;import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.app.dispatcher.Group;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;import java.util.function.Consumer;public class MainAbilitySlice extends AbilitySlice {static final HiLogLabel label = new HiLogLabel(HiLog.INFO,0x00101,"shanshui");TextField textField;Clock clock2;@Overridepublic void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);HiLog.info(label,  "hellocdtxw");HiLog.info(label,  "%{public}s World %d", "hellocdtxw", 3);HiLog.info(label,  "%{public}s World %d", "hellocdtxw", 3);System.out.println("hellocdtxw");Button btn_tongbu = (Button) findComponentById(ResourceTable.Id_btn_tongbu);textField = (TextField) findComponentById(ResourceTable.Id_text_filed_info);clock2 = (Clock) findComponentById(ResourceTable.Id_clock);clock2.setFormatIn24HourMode("yyyy-MM-dd HH:mm:ss");Button btn_yibu = (Button) findComponentById(ResourceTable.Id_btn_yibu);Button btn_delay = (Button) findComponentById(ResourceTable.Id_btn_yibu_yanchi);Button btn_group = (Button ) findComponentById(ResourceTable.Id_btn_fenzhu_task);Button btn_pingbi = (Button) findComponentById(ResourceTable.Id_btn_pingbi_task);Button btn_more_time = (Button) findComponentById(ResourceTable.Id_btn_duoci_task);btn_more_time.setClickedListener(new Component.ClickedListener() {@Overridepublic void onClick(Component component) {MoreTimesTask();}});btn_delay.setClickedListener(new Component.ClickedListener() {@Overridepublic void onClick(Component component) {YibuDealyTask();}});btn_group.setClickedListener(new Component.ClickedListener() {@Overridepublic void onClick(Component component) {YibuGroupTask();}});btn_pingbi.setClickedListener(new Component.ClickedListener() {@Overridepublic void onClick(Component component) {PingBiTask();}});btn_yibu.setClickedListener(new Component.ClickedListener() {@Overridepublic void onClick(Component component) {YibuTask();}});btn_tongbu.setClickedListener(new Component.ClickedListener() {@Overridepublic void onClick(Component component) {HiLog.info(label,  "hellocdtxw");HiLog.info(label,  "%{public}s World %d", "hellocdtxw", 3);HiLog.info(label,  "%{public}s World %d", "hellocdtxw", 3);System.out.println("hellocdtxw");TongBuTask();}});}/*同步分发任务,多线程,阻塞模式,会卡主界面,不常用*/public void TongBuTask(){TaskDispatcher dispatcher =getGlobalTaskDispatcher(TaskPriority.DEFAULT);String time_str = clock2.getText();textField.append(time_str+":同步线程启动\n");dispatcher.syncDispatch(new Runnable() {@Overridepublic void run() {try{//getUITaskDispatcher().syncDispatch(()->{// textField.append(clock2.getText()+":task1:"+Thread.currentThread().getName());//});Thread.sleep(1000);}catch (InterruptedException e){//getUITaskDispatcher().syncDispatch(()->{//  textField.append(clock2.getText()+"任务失败");// });}}});dispatcher.syncDispatch(new Runnable() {@Overridepublic void run() {try{//getUITaskDispatcher().syncDispatch(()->{// textField.append(clock2.getText()+":task1:"+Thread.currentThread().getName());//});Thread.sleep(1000);}catch (InterruptedException e){//getUITaskDispatcher().syncDispatch(()->{//  textField.append(clock2.getText()+"任务失败");// });}}});dispatcher.syncDispatch(new Runnable() {@Overridepublic void run() {try{//getUITaskDispatcher().syncDispatch(()->{// textField.append(clock2.getText()+":task1:"+Thread.currentThread().getName());//});Thread.sleep(1000);}catch (InterruptedException e){//getUITaskDispatcher().syncDispatch(()->{//  textField.append(clock2.getText()+"任务失败");// });}}});}//异步模式,鸿蒙系统app多线程public  void YibuTask(){TaskDispatcher dispatcher =getGlobalTaskDispatcher(TaskPriority.DEFAULT);String time_str = clock2.getText();textField.append(time_str+":异步线程启动\n");dispatcher.asyncDispatch(new Runnable() {@Overridepublic void run() {try{getUITaskDispatcher().asyncDispatch(()->{textField.append(clock2.getText()+":task1:"+Thread.currentThread().getName()+"\n");});Thread.sleep(1000);}catch (InterruptedException e){getUITaskDispatcher().asyncDispatch(()->{textField.append(clock2.getText()+"任务失败");});}}});dispatcher.asyncDispatch(new Runnable() {@Overridepublic void run() {try{getUITaskDispatcher().asyncDispatch(()->{textField.append(clock2.getText()+":task2:"+Thread.currentThread().getName()+"\n");});Thread.sleep(1000);}catch (InterruptedException e){getUITaskDispatcher().asyncDispatch(()->{textField.append(clock2.getText()+"任务失败");});}}});dispatcher.asyncDispatch(new Runnable() {@Overridepublic void run() {try{getUITaskDispatcher().asyncDispatch(()->{textField.append(clock2.getText()+":task3:"+Thread.currentThread().getName()+"\n");});Thread.sleep(1000);}catch (InterruptedException e){getUITaskDispatcher().asyncDispatch(()->{textField.append(clock2.getText()+"任务失败");});}}});}//异步延迟任务分发public  void YibuDealyTask(){TaskDispatcher dispatcher =getGlobalTaskDispatcher(TaskPriority.DEFAULT);String time_str = clock2.getText();dispatcher.delayDispatch(()-> {getUITaskDispatcher().asyncDispatch(()->{textField.append(time_str+":异步延迟任务分发线程启动\n");});},1000);}//异步分组任务分发public  void YibuGroupTask(){TaskDispatcher dispatcher =getGlobalTaskDispatcher(TaskPriority.DEFAULT);String time_str = clock2.getText();Group group = dispatcher.createDispatchGroup();dispatcher.asyncGroupDispatch(group, new Runnable() {@Overridepublic void run() {HiLog.info(label,  "%{public}s World %d", "hellocdtxw", 3);}});dispatcher.asyncGroupDispatch(group, new Runnable() {@Overridepublic void run() {HiLog.info(label,  "%{public}s World %d", "hellocdtxw", 3);}});dispatcher.asyncGroupDispatch(group, new Runnable() {@Overridepublic void run() {HiLog.info(label,  "%{public}s World %d", "hellocdtxw", 3);}});}//屏蔽任务分发public void PingBiTask(){TaskDispatcher dispatcher =createParallelTaskDispatcher("Dispatcher",TaskPriority.DEFAULT);Group group = dispatcher.createDispatchGroup();dispatcher.asyncGroupDispatch(group,()->{try{Thread.sleep(1000);HiLog.info(label,  "%{public}s World %d", "hellocdtxw", 3);}catch (InterruptedException ex){}});dispatcher.asyncGroupDispatch(group,()->{try{Thread.sleep(1000);HiLog.info(label,  "%{public}s World %d", "hellocdtxw", 3);}catch (InterruptedException ex){}});dispatcher.asyncDispatchBarrier(()->{try{Thread.sleep(1000);getUITaskDispatcher().asyncDispatch(()->{textField.append(clock2.getText()+":屏蔽任务分发线程启动\n");});}catch (InterruptedException ex){}});dispatcher.asyncGroupDispatch(group,()->{try{Thread.sleep(1000);HiLog.info(label,  "%{public}s World %d", "hellocdtxw", 3);}catch (InterruptedException ex){}});dispatcher.asyncGroupDispatch(group,()->{try{Thread.sleep(1000);HiLog.info(label,  "%{public}s World %d", "hellocdtxw", 3);}catch (InterruptedException ex){}});dispatcher.asyncDispatchBarrier(()->{try{Thread.sleep(1000);getUITaskDispatcher().asyncDispatch(()->{textField.append(clock2.getText()+":屏蔽任务分发线程启动\n");});}catch (InterruptedException ex){}});}public void MoreTimesTask(){getGlobalTaskDispatcher(TaskPriority.DEFAULT).applyDispatch(new Consumer<Long>() {@Overridepublic void accept(Long aLong) {getUITaskDispatcher().asyncDispatch(()->{textField.append(clock2.getText()+":多次任务分发线程启动"+aLong.toString()+"\n");});}},10); //执行10次}@Overridepublic void onActive() {super.onActive();}@Overridepublic void onForeground(Intent intent) {super.onForeground(intent);}
}

 

 

http://www.yayakq.cn/news/877761/

相关文章:

  • wordpress 快站深圳企业建站招聘
  • 西安网站开发外包制作购物网站
  • 为什么不用原来的网站做推广学网站ui设计
  • ps联盟网站北京科兴中维新冠疫苗最新消息
  • 网站建设服务商网站几种颜色
  • 网站界面风格商旅通官网app
  • 在线做印章的网站wordpress获取指定分类
  • 成都专业网站设计好公司onedrive做网站下载盘
  • 公众号第三方网站开发满亦工作室 网站建设
  • 沈阳做网站费用phpcms律师网站模板
  • 外国网站的浏览器有哪个网站专业做漫画素材的
  • 如何做网站的推广wordpress cookie伪造
  • 自己做的网站怎么才能在百度上查找延庆区加工网站建设推广
  • 创立公司网站有限责任公司优缺点
  • 毕业网站设计天眼查官网查个人
  • 公司建的是网页还是网站wordpress一登录就错位
  • 延安网站建设推广微信网站60平方旧房翻新装修要多少钱
  • 芜湖建站公司公司章程范本
  • 张家港市网站制作镇江神鹰网络科技有限公司
  • 网络营销做私活网站做网站时如何写接口文档
  • 自己做网站费用外贸网站外链
  • 杭州网站优化公司网络推广营销策划
  • 做网站什么主题好酥糖的网站建设的目的是什么
  • 成都哪家做网站建设比较好商业授权什么意思
  • 宝安专业网站设计多少钱怎么才能建立一个网站卖东西
  • 郑州商城网站建设多少钱深圳装修公司大全
  • 怎样免费注册域名海口seo快速排名优化
  • 做网站合同模板一个专门做视频配音的网站
  • 江西旺达建设工程有限公司网站360优化大师官方下载
  • 怎么做网站源码wordpress多語言主頁