线程池循环执行

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            LockObj lockObjs = new LockObj();
            List<Item> fullList = new List<Item>();
            fullList.Add(new Item(1, 2, lockObjs));
            fullList.Add(new Item(2, 3, lockObjs));
            fullList.Add(new Item(3, 2, lockObjs));
            fullList.Add(new Item(4, 3, lockObjs));
            fullList.Add(new Item(5, 2, lockObjs));
            fullList.Add(new Item(6, 3, lockObjs));
            fullList.Add(new Item(7, 3, lockObjs));
            fullList.Add(new Item(8, 2, lockObjs));
            fullList.Add(new Item(9, 3, lockObjs));
            fullList.Add(new Item(10, 2, lockObjs));

            fullList.ForEach(p => lockObjs.QueuePending.Enqueue(p));

            //加监控任务,完全靠监控对象驱动所有的任务
            Item monitor = new Item(lockObjs);
            ThreadPool.QueueUserWorkItem(new WaitCallback(monitor.Monitor), null);

            Console.Read();
        }
    }

    public class LockObj
    {
        public LockObj()
        {
            this.QueuePending = new Queue<Item>();
            this.QueueDisabled = new Queue<Item>();
            this.ActiveCount = 0;
        }

        //正处理队列  
        public Queue<Item> QueuePending { get; set; }
        //被禁用队列  
        public Queue<Item> QueueDisabled { get; set; }
        /// <summary>
        /// 活动任务数量
        /// </summary>
        public int ActiveCount { get; set; }
    }

    public class Item
    {
        #region [属性]
        public int Flag { get; set; }
        public int WaitMs { get; set; }
        public LockObj LockObj { get; set; }
        public DateTime BeginTime { get; set; }
        public DateTime EndTime { get; set; }
        public double ElaspedSeconds
        {
            get
            {
                return this.EndTime.Subtract(this.BeginTime).TotalSeconds;
            }
        }
        #endregion

        /// <summary>  
        /// 监控用构造函数  
        /// </summary>  
        /// <param name="_LockObj"></param>  
        public Item(LockObj _LockObj)
        {
            this.LockObj = _LockObj;
        }

        /// <summary>  
        /// 普通任务用的构造函数  
        /// </summary>  
        /// <param name="_Flag"></param>  
        /// <param name="_WaitSeconds"></param>  
        /// <param name="_LockQueues"></param>  
        public Item(int _Flag, int _WaitSeconds, LockObj _LockObj)
        {
            this.Flag = _Flag;
            this.WaitMs = _WaitSeconds * 1000;
            this.LockObj = _LockObj;
        }

        public void Monitor(object obj)
        {
            lock (this.LockObj)
            {
                //如果有禁用的,将其加入到等待列表队尾  
                if (this.LockObj.QueueDisabled.Count > 0)
                {
                    for (int i = this.LockObj.QueueDisabled.Count - 1; i >= 0; i--)
                    {
                        Item item = this.LockObj.QueueDisabled.Dequeue();
                        this.LockObj.QueuePending.Enqueue(item);
                        Console.WriteLine("Move Item {0} Form QueueDisabled To QueuePending", item.Flag);
                    }
                }
                //如果数量不够,则将等待列表中的加到执行列表  
                if (this.LockObj.ActiveCount < 2)
                {
                    int maxNum = 2 - this.LockObj.ActiveCount;
                    for (int i = 0; i < maxNum; i++)
                    {
                        Item item = this.LockObj.QueuePending.Dequeue();
                        ThreadPool.QueueUserWorkItem(new WaitCallback(item.Exec), null);
                    }
                    this.LockObj.ActiveCount = 2;
                }
            }
            Thread.Sleep(1000);
            ThreadPool.QueueUserWorkItem(new WaitCallback(this.Monitor), null);
        }

        public void Exec(object obj)
        {
            this.BeginTime = DateTime.Now;
            Thread.Sleep(this.WaitMs);
            this.EndTime = DateTime.Now;

            Console.WriteLine("Flag:{0}, BeginTime:{1:HH:mm:ss,ms}, EndTime: {2:HH:mm:ss,ms}, ElapsedSeconds:{3}"
                    , this.Flag < 10 ? " " + this.Flag.ToString() : this.Flag.ToString()
                    , this.BeginTime
                    , this.EndTime
                    , this.ElaspedSeconds
                    );

            lock (this.LockObj)
            {
                //模拟此对象可能遇到了异常情况
                //异常则将此对象放入到禁用队列  
                if (new Random().Next(0, 10) < 2)
                {
                    Console.WriteLine("Item : {0} is Disabled.", this.Flag);
                    this.LockObj.QueueDisabled.Enqueue(this);
                }
                //无异常则将此对象加入到队尾
                else
                {
                    this.LockObj.QueuePending.Enqueue(this);
                }
                //从队首取出对象执行  
                Item itemExec = this.LockObj.QueuePending.Dequeue();
                ThreadPool.QueueUserWorkItem(new WaitCallback(itemExec.Exec), null);
            }
        }
    }//end of class Item  
}//end of namespace

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:创作都市 设计师:CSDN官方博客 返回首页
评论

打赏作者

吉普赛的歌

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值