美文网首页
ActiveObject模式使用Command模式实现多线程do

ActiveObject模式使用Command模式实现多线程do

作者: ljt001 | 来源:发表于2023-07-10 10:20 被阅读0次

参考 《敏捷软件开发 原则、模式与实践》P.141.
原文为java代码,此处为C#代码。

ActiveObject模式是使用Command模式实现多线程的一种古老技术。

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
    public interface Command
    {
        void Execute();
    }

    public class ActiveObjectEngine
    {
        List<Command> commands = new List<Command>();
        public void AddCommand(Command command)
        {
            commands.Add(command);
        }

        public void Run()
        {
            while (commands.Any())
            {
                var command = commands.First();
                commands.Remove(command);
                command.Execute();
            }
        }
    }

    public class SleepCommand : Command
    {
        private readonly long sleepMs;
        private ActiveObjectEngine engine;
        private Command wakeupCommand;
        private bool started = false;
        private DateTime startTime = DateTime.MinValue;

        public SleepCommand(long sleepMs, ActiveObjectEngine engine, Command wakeupCommand)
        {
            this.sleepMs = sleepMs;
            this.engine = engine;
            this.wakeupCommand = wakeupCommand;
        }

        public void Execute()
        {
            var currentTime = DateTime.Now;
            if (!started)
            {
                started = true;
                startTime = currentTime;
                engine.AddCommand(this);
            }
            else if ((currentTime - startTime).TotalMilliseconds < sleepMs)
            {
                engine.AddCommand(this);
            }
            else
            {
                //Console.Write("-");
                engine.AddCommand(wakeupCommand);
            }
        }
    }

    public class DelayTyper : Command
    {
        private long delayMs;
        private char itsChar;
        private static bool stop = false;
        private static ActiveObjectEngine engine = new ActiveObjectEngine();

        static void Main(string[] args)
        {
            var stopCommand = new StopCommand();
            engine.AddCommand(new DelayTyper(100, '1'));
            engine.AddCommand(new DelayTyper(300, '3'));
            engine.AddCommand(new DelayTyper(500, '5'));
            engine.AddCommand(new DelayTyper(700, '7'));
            engine.AddCommand(new SleepCommand(20000, engine, stopCommand));

            engine.Run();
        }

        private class StopCommand : Command
        {
            public void Execute()
            {
                stop = true;
            }
        }

        public DelayTyper(long delayMs, char itsChar)
        {
            this.delayMs = delayMs;
            this.itsChar = itsChar;
        }

        public void Execute()
        {
            Console.Write(itsChar);
            if (!stop)
            {
                DelayAndRepeat();
            }
        }

        private void DelayAndRepeat()
        {
            engine.AddCommand(new SleepCommand(delayMs, engine, this));
        }
    }
}

输出结果示例

1357113115131711315113117153111311517311131511317115311131175131113151173111531113171513111315171311153111731151311131751131115311713115131117315113111531711311513117131511311175311131151317113151131171531113115173111315113171153111311751311131511731115311131715131113151713111531117311513111317511311153117131151311173151131115317113115137

相关文章

  • Java多线程ActiveObject模式

    概述 示例程序 Main 类 MakerClientThread 类 DisplayClientThread 类...

  • Balking模式

    Balking模式:“多线程版本的if”的应用场景。 1,使用synchronized实现Balking模式,这种...

  • 学习Parse iOS

    可靠上传 失败重传 上传失败 关闭App导致失败 方案(具体实现还要考虑多线程) Command 设计模式封装网络...

  • 多进程和多线程编程

    多任务的实现方式: 多进程模式 多线程模式 多进程 + 多线程 模式python即支持多进程,又支持多线程,如下进...

  • 【敏捷软件开发 原则、模式与实践】第十三到十七读书笔记

    COMMAND 模式 一个简单的 command 模式实现 代码解释 IReceiver: 一个接收者接口,act...

  • 数据结构与算法

    单例模式 使用场景 实现方法 多线程实现方式http://xiaorui.cc/archives/3165http...

  • Python 学习笔记10 - 进程 Process 和线程 T

    多任务的实现有3种方式: 多进程模式; 多线程模式; 多进程+多线程模式。 多进程 Unix/Linux操作系统提...

  • Juypter快捷键 & 设置

    Juypter 分为编辑(edit)模式以及命令(command)模式。在编辑模式下,使用Esc可以退出并进入命令...

  • 行为型-Interpreter

    解释器模式的原理和实现 命令模式的原理解读命令模式的英文翻译是 Command Design Pattern。在 ...

  • 单例模式

    单例模式 单例模式:用来保证一个对象只能被创建一次。 普通版 代码实现如下 同步锁单例 单例模式如果再多线程中使用...

网友评论

      本文标题:ActiveObject模式使用Command模式实现多线程do

      本文链接:https://www.haomeiwen.com/subject/pdeeudtx.html