Пример #1
0
        /// <summary>
        /// 添加新项到时间轮上
        /// </summary>
        /// <param name="itemId"></param>
        /// <param name="delaySeconds"></param>
        /// <returns></returns>
        public bool Add(long itemId, int delaySeconds)
        {
            if (_itemIds.Contains(itemId))
            {
                return(false);
            }
            _itemIds.Add(itemId);
            //如果小于1s,添加不成功
            if (delaySeconds < 1)
            {
                return(false);
            }
            var cyNum  = delaySeconds / _slotNum;
            var offset = delaySeconds - (cyNum * _slotNum);
            var index  = 0;

            if (offset + _currentIndex >= _slotNum)
            {
                index = (_currentIndex + offset) - _slotNum;
            }
            else
            {
                index = _currentIndex + offset;
            }

            var item = new TWItem
            {
                Id    = itemId,
                CyNum = cyNum
            };
            var itemSet = _itemsSet[index];

            if (itemSet == null)
            {
                lock (this)
                {
                    itemSet = _itemsSet[index];
                    if (itemSet == null)
                    {
                        itemSet          = new HashSet <TWItem>();
                        _itemsSet[index] = itemSet;
                    }
                }
            }
            lock (this)
            {
                return(itemSet.Add(item));
            }
        }
Пример #2
0
 public bool Equals(TWItem item)
 {
     return(item.Id == Id);
 }