using System.Collections.Concurrent;
using System.Threading.Tasks;
public class InstanceManager<T> where T : class
{
private readonly ConcurrentQueue<T> _queue = new();
public InstanceManager(IEnumerable<T> initialInstances)
{
foreach (var instance in initialInstances)
{
_queue.Enqueue(instance);
}
}
public T GetUnusedInstance()
{
if (_queue.TryDequeue(out var instance))
{
// 假设 T 有一个 IsUsed 属性用于标记
if (instance is IUsedMarker marker)
{
marker.IsUsed = true;
}
return instance;
}
return null;
}
}
// 示例接口
public interface IUsedMarker
{
bool IsUsed { get; set; }
}