日期:2014-05-18 浏览次数:20785 次
public class MyQueue<T> { object _mutex = new object(); List<T> _container = new List<T>(); public int Length { get{ return this._container.Count; } } public void Enqueue(T item) { lock (this._mutex) { this._container.Add(item); } } public T Dequeue() { T t = default(T); lock (this._mutex) { if (this._container.Count > 0) { t = this._container[0]; this._container.Remove(t); } else throw new Exception("Not has item."); } return t; } 调用 MyQueue<int> q = new MyQueue<int>(); q.Enqueue(1); q.Enqueue(2); q.Enqueue(3); q.Enqueue(4); int length = q.Length; for (int i = 0; i < length; ++i) Console.WriteLine(q.Dequeue());