首页 > 编程语言 >C# 实现任务池

C# 实现任务池

时间:2024-01-30 22:32:13浏览次数:23  
标签:C# private 实现 任务 线程 action new public ActionState

项目中,当需要使用多线程时,习惯用Task 或者Thread去开启线程,当需要响应速度时,Task总是不那么让人满意。

建立任务池,多个线程在循环执行,当有任务过来时,就立即使用空闲的线程去执行任务,以达到快速响应的效果。

以下是任务池的设计代码:

使用中有问题,或有优化的地方,可以在评论区提出,感谢!

------------------------------------------------------------------------------------------------------------------------

可以直接引用Nuget包  LS.Helper   即可使用     或直接使用源码也可以(往下拉)

C# 实现任务池_任务池

使用示例:

/// <summary>
        /// 用Thread运行方法 
        /// 一般用于整个生命周期的线程
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public static Thread RunByThread(Action action)
        {
            Thread _t = new Thread(() => { action.Invoke(); }) { IsBackground = true };
            _t.Start();
            return _t;
        }
 
        /// <summary>
        /// 使用任务池运行方法
        /// 一般用于临时运行或者周期性运行的方法
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public static BaseResult RunByActionPool(Action action)
        {
            return ACPool.AddAction(action);
        }
 
        private static readonly object locker = new object();
        private static ActionPool _acPool;
        /// <summary>
        /// 任务池对象
        /// </summary>
        public static ActionPool ACPool
        {
            get
            {
                lock (locker)
                {
                    if (_acPool == null)
                    {
                        _acPool = new ActionPool();
                        _acPool.InitThread();
                    }
                    return _acPool;
                }
            }
        }

✍更新说明

-----2023年09月11日--------------------------------------------------------------------------------------------------------------

1.增加Waiting状态,当SetAction()时,状态立即变更为Waiting

2.SetAction()前再次对线程状态做确认,确保数据稳定


下面是实现源码:

/// <summary>
    /// 任务池
    /// 目前支持无返回值的方法执行
    /// </summary>
    public class ActionPool
    {
        /// <summary>
        /// 线程总数
        /// </summary>
        public int PoolCount = 50;
        /// <summary>
        /// 待执行的方法集合
        /// </summary>
        private ConcurrentQueue<Action> actions = new ConcurrentQueue<Action>();
        /// <summary>
        /// 标识是否运行任务池
        /// </summary>
        private bool isRunThread = false;
        /// <summary>
        /// 线程列表
        /// </summary>
        private List<ActionThread> threads = new List<ActionThread>();
        /// <summary>
        /// 轮询线程
        /// </summary>
        private Thread _thread;
        /// <summary>
        /// 日志信息输出 委托
        /// </summary>
        public delegate void DelegateLog(string log);
 
        /// <summary>
        /// 日志信息输出
        /// </summary>
        public event DelegateLog SendLog;
 
        /// <summary>
        /// 任务池
        /// </summary>
        public ActionPool()
        {
 
        }
 
        /// <summary>
        /// 任务池初始化
        /// </summary>
        /// <param name="poolCount">线程总数</param>
        public void InitThread(int poolCount = 50)
        {
            try
            {
                PoolCount = poolCount;
                if (!isRunThread)
                {
                    isRunThread = true;
                    threads = new List<ActionThread>();
                    for (int i = 0; i < PoolCount; i++)
                    {
                        var at = new ActionThread();
                        at.SendLog += At_SendLog;
                        threads.Add(at);
                    }
 
                    _thread = new Thread(Execute) { IsBackground = true };
                    _thread.Start();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        private void At_SendLog(string log)
        {
            SendLog?.Invoke(log);
        }
 
        /// <summary>
        /// 当有任务进来时,选择空闲的线程去执行任务
        /// </summary>
        private void Execute()
        {
            while (isRunThread)
            {
                try
                {
                    if (actions.Count > 0)
                    {
                        var index = threads.FindIndex(x => x.IsReady());
                        if (index >= 0)
                        {
                            var t = threads[index];
                            //再次确认是否可以添加任务进来
                            if (t.IsReady())
                            {
                                if (actions.TryDequeue(out Action action))
                                    t.SetAction(action);
                            }
                        }
                    }
                }
                catch (Exception ex)
                { }
                Thread.Sleep(10);
            }
        }
 
        /// <summary>
        /// 添加执行方法到任务池
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public BaseResult AddAction(Action action)
        {
            try
            {
                if (action == null)
                    return new BaseResult(false, "Action 不能为Null");
 
                actions.Enqueue(action);
                return BaseResult.Successed;
            }
            catch (Exception ex)
            {
                return new BaseResult(false, ex.ToString());
            }
        }
 
        /// <summary>
        /// 结束线程池
        /// </summary>
        public void Stop()
        {
            try
            {
                isRunThread = false;
                if (threads.Count > 0)
                {
                    foreach (var t in threads)
                    {
                        try
                        {
                            t.Stop();
                        }
                        catch (Exception ex) { }
                    }
                }
                _thread?.Join();
            }
            catch (Exception ex)
            {
 
            }
        }
    }
 
    /// <summary>
    /// 工作线程对象
    /// </summary>
    public class ActionThread
    {
        /// <summary>
        /// 线程对象
        /// </summary>
        private Thread _thread;
        /// <summary>
        /// 工作状态
        /// </summary>
        public ActionState ActionStatus;
        /// <summary>
        /// 需要执行的方法
        /// </summary>
        private Action _action;
        /// <summary>
        /// 标识是否运行线程
        /// </summary>
        private bool isRunThread = false;
        /// <summary>
        /// 循环线程的睡眠时间
        /// </summary>
        private int _sleeptime = 500;
        /// <summary>
        /// 锁对象
        /// </summary>
        private static readonly object _lock = new object();
        /// <summary>
        /// 日志信息输出 委托
        /// </summary>
        public delegate void DelegateLog(string log);
 
        /// <summary>
        /// 日志信息输出
        /// </summary>
        public event DelegateLog SendLog;
 
        public ActionThread(int sleeptime = 500)
        {
            ActionStatus = ActionState.Init;
            _sleeptime = sleeptime;
            _action = null;
            isRunThread = true;
            _thread = new Thread(Execute) { IsBackground = true };
            _thread.Start();
            ActionStatus = ActionState.Ready;
        }
 
        /// <summary>
        /// 判断线程是否有空闲
        /// </summary>
        /// <returns></returns>
        public bool IsReady()
        {
            return (ActionStatus == ActionState.Ready && _action == null);
        }
 
        /// <summary>
        /// 执行任务--循环方法
        /// </summary>
        public void Execute()
        {
            while (isRunThread)
            {
                try
                {
                    lock (_lock)
                    {
                        if ((ActionStatus == ActionState.Ready|| ActionStatus==ActionState.Waiting) && _action != null)
                        {
                            ActionStatus = ActionState.Working;
                            DateTime begin = DateTime.Now;
                            SendLog?.Invoke($"ID[{_thread.ManagedThreadId}] 开始执行Action[{_action.Method.Name}]");
                            //_action.Invoke();
                            var res = _action.BeginInvoke((ar) =>
                            {
                                _action = null;
                                ActionStatus = ActionState.Ready;
                            }, null);
                            //_action.EndInvoke(res);
                            SendLog?.Invoke($"ID[{_thread.ManagedThreadId}] 完成执行Action[{_action.Method.Name}] 耗时:{DateTime.Now.Subtract(begin).TotalMilliseconds}");
                        }
                    }
                }
                catch (Exception ex)
                {
 
                }
                finally
                {
                }
                Thread.Sleep(_sleeptime);
            }
        }
 
        /// <summary>
        /// 插入任务
        /// </summary>
        /// <param name="action"></param>
        public BaseResult SetAction(Action action)
        {
            try
            {
                if (ActionStatus == ActionState.Ready)
                {
                    _action = action;
                    ActionStatus= ActionState.Waiting;
                    return BaseResult.Successed;
                }
                return new BaseResult(false, "线程正忙,无法添加任务");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        /// <summary>
        /// 停止线程工作
        /// </summary>
        public void Stop()
        {
            try
            {
                isRunThread = false;
                _action = null;
                ActionStatus = ActionState.Init;
                Thread.Sleep(1000);
                _thread?.Join();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
 
    /// <summary>
    /// 线程工作状态
    /// </summary>
    public enum ActionState
    {
        /// <summary>
        /// 初始化中
        /// </summary>
        Init,
        /// <summary>
        /// 待开始
        /// </summary>
        Ready,
        /// <summary>
        /// 等待开始
        /// </summary>
        Waiting,
        /// <summary>
        /// 任务执行中
        /// </summary>
        Working
    }


标签:C#,private,实现,任务,线程,action,new,public,ActionState
From: https://blog.51cto.com/u_16544892/9495900

相关文章

  • 2024/1/30 scala、Flink
    scala的模式匹配:很强大,也很难记住。和java相比,代码简洁了,但是熟练难度却上去了。 各种花里胡哨的模式匹配。还有很多至简原则。无疑是给新手带来了很多的麻烦。 这个scala给人的印象是:java?python?js?的集大成者???......
  • http encode 编码
    uses System.NetEncodingvars:string:=TNetEncoding.URL.Encode('123');//123vars2:string:=TNetEncoding.URL.Encode('abcd');//abcdvars3:string:=TNetEncoding.URL.Encode('ab1cd2');//ab1cd2vars4:string:=TNetEn......
  • 初中英语优秀范文100篇-074I want to invent a flying car-我想发明一辆飞行汽车
    PDF格式公众号回复关键字:SHCZFW074记忆树1Nevercanwedenythathigh-technologyhasafar-reachinginfluenceonourlife.翻译我们不能否认高科技对我们的生活产生了深远的影响简化记忆影响句子结构Nevercanwedeny是倒装句,正常语序是“Wecanneverdenyt......
  • chrome驱动 selenium
    chrome驱动selenimchromedriver下载地址:ChromeDriver-WebDriverforChrome-Downloads官网下载地址: https://chromedriver.com/官网下载地址114版本以下:ChromeDriver114andolderversionsdownloadChromeDriverDownloadReleaseStable/Beta/Dev/Canary......
  • CSAPP学习笔记——chapter5 优化程序性能
    编写高效程序需要做到以下几点:第一,我们必须选择一组适当的算法和数据结构第二,我们必须编写出编译器能够有效优化以转换成高效可执行代码的源代码。对于这第二点,理解优化编译器的能力和局限性是很重要的。编写程序方式中看上去只是一点小小的变动,都会引起编译器优化方式很大的变化......
  • Debug: mysql_real_connect failed: errno: , error
    [ERROR:mysql_real_connectfailed:errno:,error]kubectllogstransform-pod-name-nkubeflow-->INFO:absl:MetadataStorewithgRPCconnectioninitializedWARNING:absl:mlmdclientInternalError:mysql_real_connectfailed:errno:,error:ERROR:absl:......
  • WC2024 Lectures
    大概只会有例题题解。目录P8263「YnoiEasyRound2020」TEST_8P8263「YnoiEasyRound2020」TEST_8Tag:S-持久化WBLT。使用WBLT来维护整个括号序列,则三四操作已经做完了。考虑一二操作,使用倍增的方式处理出复制\([l,r]\)区间的结果,于是可以在\(O(\logk)\)的复杂度内......
  • 【侯捷C++面向对象笔记】补充5-new & delete重载
    平时所使用的new和delete操作,称之为表达式,一般由好几个步骤组成。如上图所示,new表达式会被编译器转化为三个步骤。new表达式不能重载,但其中operatornew是可以重载的。➡️全局::operatornew的重载why不能放在namespace内?因为全局operatornew是放在defaultglobalnamespac......
  • informer cache自定义索引
    informercache默认通过namespace/name作为key把对象保存到map中。条件查询时一般通过labels.Selector来过滤,但这需要遍历所有元素,informercache可以类似于MySQL那样建立索引,来提高查询速度。//map根据指定的key来给对象分类//IndexFuncknowshowtocomputethesetofind......
  • AtCoder Beginner Contest 338
    ABC338总结A-Capitalized?翻译给你一个由大写和小写英文字母组成的非空字符串\(S\)。请判断是否满足以下条件:\(S\)的第一个字符是大写字母,其他所有字符都是小写字母。如果满足,输出Yes,否则输出No。分析按题目说的判断即可。code#include<bits/stdc++.h>usingn......