zoukankan      html  css  js  c++  java
  • HarmonyOS 多线程

    1.异步

    2.同步

    3.任务组

    一  异步

     /**
         * 异步
         */
        private void click1() {
            HiLog.info(hiLogLabel, "click1:start");
            TaskDispatcher dispatcher = getGlobalTaskDispatcher(TaskPriority.DEFAULT);
            dispatcher.asyncDispatch(new Runnable() {
                @Override
                public void run() {
                    HiLog.info(hiLogLabel, "click1:doing1");
                }
            });
    
            dispatcher.asyncDispatch(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    HiLog.info(hiLogLabel, "click1:doing2");
                }
            });
    
            dispatcher.asyncDispatch(new Runnable() {
                @Override
                public void run() {
                    HiLog.info(hiLogLabel, "click1:doing3");
                }
            });
    
        }

    二  同步

    /**
         * 同步
         */
        private void click2() {
            HiLog.info(hiLogLabel, "click2:start");
            TaskDispatcher dispatcher = getGlobalTaskDispatcher(TaskPriority.DEFAULT);
            dispatcher.syncDispatch(new Runnable() {
                @Override
                public void run() {
                    HiLog.info(hiLogLabel, "click2:doing1");
                }
            });
            HiLog.info(hiLogLabel, "click2:end1");
            dispatcher.syncDispatch(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    HiLog.info(hiLogLabel, "click2:doing2");
                }
            });
            HiLog.info(hiLogLabel, "click2:end2");
            dispatcher.syncDispatch(new Runnable() {
                @Override
                public void run() {
                    HiLog.info(hiLogLabel, "click2:doing3");
                }
            });
            HiLog.info(hiLogLabel, "click2:end3");
        }

    三  任务组

    /**
    * 任务组
    */
    private void click3() {
    HiLog.info(hiLogLabel, "click3:start----");
    TaskDispatcher dispatcher = getGlobalTaskDispatcher(TaskPriority.DEFAULT);
    Group group = dispatcher.createDispatchGroup();
    dispatcher.asyncGroupDispatch(group, new Runnable() {
    @Override
    public void run() {
    HiLog.info(hiLogLabel, "click3:doing1");
    }
    });

    dispatcher.asyncGroupDispatch(group, new Runnable() {
    @Override
    public void run() {
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    HiLog.info(hiLogLabel, "click3:doing2");
    }
    });

    dispatcher.asyncGroupDispatch(group, new Runnable() {
    @Override
    public void run() {
    HiLog.info(hiLogLabel, "click3:doing3");
    }
    });

    //前面3个执行完了,才会执行
    dispatcher.groupDispatchNotify(group, new Runnable() {
    @Override
    public void run() {
    HiLog.info(hiLogLabel, "click3:doing4-groupDispatchNotify");
    }
    });
    HiLog.info(hiLogLabel, "click3:start----end");
    }
  • 相关阅读:
    洛谷【P1480】A/B Problem
    bzoj 2654 && bzoj 3675 总结
    关于三维莫队问题的一些思考和探究
    BZOJ 1179 抢掠计划atm (缩点+有向无环图DP)
    BZOJ 1500 Luogu P2042 [NOI2005] 维护数列 (Splay)
    Codeforces 919D Substring (拓扑图DP)
    【学习笔记】有向无环图上的DP
    【学习笔记】求解简单递归式的一般方法
    BZOJ 3930 Luogu P3172 选数 (莫比乌斯反演)
    POJ 1061 BZOJ 1477 Luogu P1516 青蛙的约会 (扩展欧几里得算法)
  • 原文地址:https://www.cnblogs.com/ligenyun/p/15651943.html
Copyright © 2011-2022 走看看