Example #1
0
        /// <summary>
        /// 压入一个时间流继承对象
        /// </summary>
        /// <param name="tf"></param>
        /// <param name="tfIndex">数组前两个线程是给框架使用,0负责数据部分 1负责文件部分 2 单线程同步update</param>
        internal void PushTimeFlow(BaseTimeFlow tf, int tfIndex = -1)
        {
            // 查找适用的时间流存储器
            int minQueueTaskTfCount = int.MaxValue;
            int index = tfIndex;

            lock (m_lock)
            {
                if (tfIndex == -1)
                {
                    // 按单核算 最高为4 索引位最高为3 否则会出问题
                    for (int i = 3, len = timeFlowThreads.Count; i < len; i++)
                    {
                        var count = timeFlowThreads[i].GetTaskCount();
                        if (count < minQueueTaskTfCount)
                        {
                            minQueueTaskTfCount = count;
                            index = i;
                        }
                    }
                }
                // 如果线程没有启动则启动
                var timeFlows = timeFlowThreads[index];
                if (!timeFlows.IsRunning)
                {
                    timeFlows.Start();
                }
                if (timeFlows == null)
                {
                    timeFlows = timeFlowThreads[timeFlowThreads.Count - 1];
                }
                // 压入操作
                timeFlows.Push(tf);
            }
        }
Example #2
0
 internal void Push(BaseTimeFlow timeFlow)
 {
     lock (m_lock) timeFlows.Add(timeFlow);
 }
Example #3
0
        /// <summary>
        /// 更新句柄
        /// 这个地方要优化,在原基础线程优化方案上改成自动增长的模式,检测线程里工作线数量与处理时长的比例是否对称和目标延迟是否对等,否则增加新的线程并且移动到新线程中
        /// 以及线程超时优化
        /// </summary>
        private void UpdateHandle()
        {
            // 时间补偿助手
            TimeFix timeFixHelper = new TimeFix(TimeFlowManager.timeFlowPeriod);
            // 闲置处理时间计数, 1000次为10s 如果超出10s空处理则关闭线程
            int idlHandleTimeCount = 0;
            // 计算句柄超出间隔次数 1s内连续超出10次则分割任务
            int mathHandleTimeCount = 0;
            // 重置计算句柄超出间隔次数计数 100次为1s
            int mathHandleTimeResetCount = 0;
            // 暂停推送任务计数 6000次为60s 超出1分钟尝试重新接管线程
            int pausePushTaskCount = 0;

            // 转移其他线程组
            BaseTimeFlow[] moveOtherThreadFlow = null;

            int currentPeriod = TimeFlowManager.timeFlowPeriod;

            while (IsRunning)
            {
                timeFixHelper.Begin();
                lock (m_lock)
                {
                    int totalTime = 0;
                    var len       = timeFlows.Count;
                    for (int i = len - 1; i >= 0; i--)
                    {
                        BaseTimeFlow tf = timeFlows[i];
                        if (tf.IsTimeUpdateActive())
                        {
                            if (tf.isTimeFlowStop)
                            {
                                timeFlows.RemoveAt(i);
                                tf.UpdateEndES();
                            }
                            else if (!tf.isTimeFlowPause)
                            {
                                tf.UpdateES(currentPeriod);
                                totalTime += tf.lastUseTime;
                            }
                        }
                    }

                    // 重置超时检测
                    Interlocked.Exchange(ref threadBlockTimeOutCount, 0);

                    // 无任务进行则关闭
                    if (len <= 0)
                    {
                        // 超出闲置时间跳出循环
                        if (++idlHandleTimeCount >= 1000)
                        {
                            break;
                        }
                    }
                    else
                    {
                        if (idlHandleTimeCount > 0)
                        {
                            idlHandleTimeCount = 0;
                        }
                        // index大于等于3为0 1 2核心线程不需要处理分离任务
                        if (Index >= 3)
                        {
                            // 超出运行算率3次 分割算率建立新时间线
                            if (len > 1 && totalTime > TimeFlowManager.timeFlowPeriod)
                            {
                                if (++mathHandleTimeCount >= 10)
                                {
                                    mathHandleTimeCount = 0;
                                    IsPausePushTask     = true;

                                    // 移除一半内容进入新的时间线
                                    int moveOtherThreadFlowIndex = 0;
                                    moveOtherThreadFlow = new BaseTimeFlow[len - len / 2];
                                    for (int i = len - 1, end = len / 2; i >= end; i--)
                                    {
                                        BaseTimeFlow tf = timeFlows[i];
                                        if (tf.IsTimeUpdateActive())
                                        {
                                            moveOtherThreadFlow[moveOtherThreadFlowIndex++] = tf;
                                            timeFlows.RemoveAt(i);
                                        }
                                    }
                                }

                                // 超过1s 重置一次分割检测任务
                                if (++mathHandleTimeResetCount >= 100)
                                {
                                    mathHandleTimeResetCount = 0; mathHandleTimeCount = 0;
                                }
                            }

                            // 暂停接管任务缓和处理
                            if (IsPausePushTask)
                            {
                                if (++pausePushTaskCount >= 6000)
                                {
                                    pausePushTaskCount = 0;
                                    IsPausePushTask    = false;
                                }
                            }
                        }
                    }
                }
                // 在锁外迁移
                if (moveOtherThreadFlow != null)
                {
                    // 创建新的时间线
                    var index = TimeFlowManager.Instance.CreateExtraTimeFlow();
                    for (int i = 0, len = moveOtherThreadFlow.Length; i < len; i++)
                    {
                        TimeFlowManager.Instance.PushTimeFlow(moveOtherThreadFlow[i], index);
                    }
                    moveOtherThreadFlow = null;
                }

                Thread.Sleep(currentPeriod);
                currentPeriod = timeFixHelper.End();
            }
            // 线程结束时则重置为false
            Close();
        }