kem*_*002 21
只是为了记录,循环的定义:
http://en.wikipedia.org/wiki/Round-robin_scheduling
只需使用队列.取一个顶部,使用它并把它放回去.这确保了最近使用的一个将始终是最后一个被拾取的.
Queue<Server> q = new Queue<Server>();
//get the next one up
Server s = q.DeQueue();
//Use s;
//put s back for later use.
q.Enqueue(s);
Run Code Online (Sandbox Code Playgroud)
链接到队列类:
http://msdn.microsoft.com/en-us/library/7977ey2c.aspx
与ebpower相同的想法,但关注的是下一个项目是什么,而不是下一个项目的索引.
public class RoundRobinList<T>
{
private readonly IList<T> _list;
private readonly int _size;
private int _position;
public RoundRobinList(IList<T> list)
{
if (!list.Any())
throw new NullReferenceException("list");
_list = new List<T>(list);
_size = _list.Count;
}
public T Next()
{
if (_size == 1)
return _list[0];
Interlocked.Increment(ref _position);
var mod = _position % _size;
return _list[mod];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14314 次 |
| 最近记录: |