namespace MeloongCore; /// /// 使用粗粒度锁的线程安全 实现。 /// 通常建议使用 或其他线程安全集合,它们具有更好的性能。 /// public class ConcurrentList : IList, IList, IEnumerable, IEnumerable { private readonly List _items = []; // 构造函数 public ConcurrentList() { } public ConcurrentList(IEnumerable data) => _items = new(data); public static implicit operator ConcurrentList(List data) => new(data); public static implicit operator List(ConcurrentList data) { lock (data.SyncRoot) { return new(data._items); } } // 枚举器 /// /// 返回此列表的副本。 /// public IEnumerator GetEnumerator() { lock (SyncRoot) return _items.ToList().GetEnumerator(); } /// /// 返回此列表的副本。 /// IEnumerator IEnumerable.GetEnumerator() { lock (SyncRoot) return _items.ToList().GetEnumerator(); } // 成员 public int Count { get { lock (SyncRoot) { return _items.Count; } } } public bool IsReadOnly => false; bool IList.IsFixedSize => false; bool ICollection.IsSynchronized => true; public readonly object SyncRoot = new(); object ICollection.SyncRoot => SyncRoot; public T this[int index] { get { lock (SyncRoot) { return _items[index]; } } set { lock (SyncRoot) { _items[index] = value; } } } object? IList.this[int index] { get => this[index]; set { lock (SyncRoot) { _items[index] = (T) value!; } } } public void Add(T item) { lock (SyncRoot) { _items.Add(item); } } int IList.Add(object? value) { lock (SyncRoot) { _items.Add((T) value!); return _items.Count - 1; } } public void Insert(int index, T item) { lock (SyncRoot) { _items.Insert(index, item); } } void IList.Insert(int index, object? value) { lock (SyncRoot) { _items.Insert(index, (T) value!); } } public bool Remove(T item) { lock (SyncRoot) { return _items.Remove(item); } } void IList.Remove(object? value) { lock (SyncRoot) { if (value is T t) _items.Remove(t); } } public void RemoveAt(int index) { lock (SyncRoot) { _items.RemoveAt(index); } } public void Clear() { lock (SyncRoot) { _items.Clear(); } } public bool Contains(T item) { lock (SyncRoot) { return _items.Contains(item); } } bool IList.Contains(object? value) { lock (SyncRoot) { return value is T t && _items.Contains(t); } } public int IndexOf(T item) { lock (SyncRoot) { return _items.IndexOf(item); } } int IList.IndexOf(object? value) { lock (SyncRoot) { return value is T t ? _items.IndexOf(t) : -1; } } public void CopyTo(T[] array, int arrayIndex) { lock (SyncRoot) { _items.CopyTo(array, arrayIndex); } } void ICollection.CopyTo(Array array, int index) { lock (SyncRoot) { ((ICollection) _items).CopyTo(array, index); } } }