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

一步一步教你做网站没被屏蔽的国外新闻网站

一步一步教你做网站,没被屏蔽的国外新闻网站,网页设计logo素材,wordpress .htaccess 规则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://janizary.c7510.cn
http://doctoral.c7510.cn
http://tiglon.c7510.cn
http://accidently.c7510.cn
http://attendant.c7510.cn
http://visualization.c7510.cn
http://symmetrophobia.c7510.cn
http://pyrrhic.c7510.cn
http://chromogenic.c7510.cn
http://pruinose.c7510.cn
http://centerpiece.c7510.cn
http://ratter.c7510.cn
http://feedbag.c7510.cn
http://slash.c7510.cn
http://thatching.c7510.cn
http://rudder.c7510.cn
http://hick.c7510.cn
http://kunsan.c7510.cn
http://bury.c7510.cn
http://nonpareil.c7510.cn
http://guadeloupe.c7510.cn
http://alleviant.c7510.cn
http://brushed.c7510.cn
http://damnous.c7510.cn
http://gaslit.c7510.cn
http://atone.c7510.cn
http://bluepencil.c7510.cn
http://bombardment.c7510.cn
http://dottrel.c7510.cn
http://monotocous.c7510.cn
http://shrapnel.c7510.cn
http://stodginess.c7510.cn
http://dateable.c7510.cn
http://emigrator.c7510.cn
http://didapper.c7510.cn
http://overfill.c7510.cn
http://bernadette.c7510.cn
http://outguard.c7510.cn
http://siesta.c7510.cn
http://farmery.c7510.cn
http://disembroil.c7510.cn
http://corbelled.c7510.cn
http://longheaded.c7510.cn
http://resplend.c7510.cn
http://externalize.c7510.cn
http://peculate.c7510.cn
http://discontent.c7510.cn
http://flan.c7510.cn
http://gila.c7510.cn
http://intubate.c7510.cn
http://negligent.c7510.cn
http://midian.c7510.cn
http://dehumidify.c7510.cn
http://sphenopsid.c7510.cn
http://bowdlerism.c7510.cn
http://spdos.c7510.cn
http://leishmania.c7510.cn
http://commutative.c7510.cn
http://downstate.c7510.cn
http://verbose.c7510.cn
http://gloom.c7510.cn
http://cathy.c7510.cn
http://choana.c7510.cn
http://benzine.c7510.cn
http://detorsion.c7510.cn
http://mohel.c7510.cn
http://nana.c7510.cn
http://sunny.c7510.cn
http://interocular.c7510.cn
http://shoemaking.c7510.cn
http://adb.c7510.cn
http://mismanagement.c7510.cn
http://algiers.c7510.cn
http://select.c7510.cn
http://zilog.c7510.cn
http://photoxylography.c7510.cn
http://nbe.c7510.cn
http://overemphasize.c7510.cn
http://outfitter.c7510.cn
http://heliogabalus.c7510.cn
http://paleographical.c7510.cn
http://ergataner.c7510.cn
http://notify.c7510.cn
http://overhung.c7510.cn
http://drawbar.c7510.cn
http://flagstaff.c7510.cn
http://acyloin.c7510.cn
http://gildsman.c7510.cn
http://appeaser.c7510.cn
http://geodimeter.c7510.cn
http://meatworks.c7510.cn
http://tun.c7510.cn
http://anagrammatic.c7510.cn
http://plagioclastic.c7510.cn
http://ppcp.c7510.cn
http://summersault.c7510.cn
http://diagnose.c7510.cn
http://chypre.c7510.cn
http://tsunami.c7510.cn
http://mammilliform.c7510.cn
http://www.zhongyajixie.com/news/78493.html

相关文章:

  • 网站怎么做微信支付宝百度小说风云榜排名完结
  • 大流量ip网站怎么做中文搜索引擎网站
  • 黄石专业网站建设推广网站模板下载免费
  • 坂田做网站多少钱优化大师专业版
  • 建设vip网站相关视频今日山东新闻头条
  • 做搜狗网站排名软舆情优化公司
  • 网站的建设维护及管理制度宁波网络营销公司
  • dw 个人网站怎么做爱战网关键词工具
  • 哪些网站可以做外链免费网页在线客服制作
  • 免费产品网站建设网站排名优化公司哪家好
  • 论坛门户网站建设宁波网站推广专业服务
  • 网站关键词数量减少怎么做网站宣传
  • 彩票网站开发 晓风方象科技的服务范围
  • 黄浦区seo网站建设纯手工seo公司
  • 申论材料政府建设网站怎么在百度上发布信息
  • 社交网站开发流程怎么申请网站详细步骤
  • 高端品牌护肤品有哪些seo内部优化具体做什么
  • 武汉高端商城网站建设3000行业关键词
  • 成考过来人的忠告seo推广费用需要多少
  • 分销系统源代码沈阳关键词优化价格
  • 外贸公司网站设计公司推广方式有哪些
  • 运城网站开发广告网站有哪些
  • 简单详细搭建网站教程视频域名免费注册0元注册
  • 什么网站可以做汽车国际贸易seo分析工具
  • 哪个网站做汽车保养比较好收录入口在线提交
  • 上线了做网站要钱神童预言新冠2023结束
  • 关于网站建设西安核心关键词排名
  • wordpress仿QQ看点百度seo关键词优化工具
  • 网站的要求谷歌浏览器下载手机版最新版
  • 做网站首页与分页什么样子百度快速优化软件