具有semaphoreSlim的模式WaitAllOneByOne,此实现线程安全吗?

时间:2018-09-21 03:11:21

标签: c# multithreading asynchronous thread-safety task-parallel-library

我有两个任务要执行,一个任务要获取数据,大约需要2秒钟才能完成,另一个任务要处理数据,大约需要3秒钟。我必须针对大约13.000个不同的元素运行这两个任务。如果我安全地运行此任务,则大约需要18个小时才能完成。

现在我正在考虑使用模式WaitAllOneByOne,但是在每个元素上调用Task.Run会使我的数据库崩溃,因此我想到了实现SemaphoreSlim来控制每个任务上应该运行多少个线程。

到目前为止,我已经能够创建该模式的实现,但是我不知道它是否是线程安全的,并且我对如何对其进行测试一无所知

代码如下:

class Program
{
    static async Task Main(string[] args)
    {
        List<Guid> idsCombinacion = GetIdsCombinacion(10);
        List<Task<string>> todo = new List<Task<string>>(idsCombinacion.Count);
        List<Task> processing = new List<Task>(idsCombinacion.Count);

        //this starts callings to GetData, up to 2 threads.
        SemaphoreSlim semaphoreGetData = new SemaphoreSlim(2);
        var dataTask = GetDataAsync(idsCombinacion, todo, semaphoreGetData);

        SemaphoreSlim semaphoreProcessData = new SemaphoreSlim(3);
        while (todo.Count > 0)
        {
            Task<string>[] tasks = todo.ToArray();
            int idx = Task.WaitAny(tasks);
            todo.RemoveAt(idx);

            //once the tasks in the to-do list goes finishing, we start a new task to perform the Processing, up to 3 threads
            semaphoreProcessData.Wait();
            var result = tasks[idx].Result;
            tasks[idx].Dispose();
            var t = Task.Factory.StartNew(() =>
            {
                Task.Delay(3000).Wait();
                Console.WriteLine($"todo: {todo.Count}; processing: {processing.Count}; guid: {result}");
                semaphoreProcessData.Release();
            }, TaskCreationOptions.LongRunning);
            processing.Add(t);

        }

        var aux = processing.ToArray();
        //we wait for all the jobs to complete
        Task.WaitAll(aux);

        await dataTask;

        semaphoreGetData.Dispose();
        semaphoreProcessData.Dispose();
    }

    private static async Task GetDataAsync(List<Guid> idsCombinacion, List<Task<string>> todo, SemaphoreSlim semaphoreGetData)
    {
        foreach (var idComb in idsCombinacion)
        {
            await semaphoreGetData.WaitAsync();
            var t = Task<string>.Factory.StartNew(() =>
            {
                Task.Delay(2000).Wait();
                semaphoreGetData.Release();
                return "Guid: " + idComb;
            }, TaskCreationOptions.LongRunning);
            todo.Add(t);
        }
    }


    private static List<Guid> GetIdsCombinacion(int howMany)
    {
        var idsCombinacion = new List<Guid>(howMany);
        for (int i = 0; i < howMany; i++)
            idsCombinacion.Add(Guid.NewGuid());
        return idsCombinacion;
    }
}   

Joe Hummel博士在此pluralsight course中的模式WaitAllOneByOne。

0 个答案:

没有答案