使用多线程处理List数据

最近遇到了一个业务场景,需要对List中的数据逐个发起http请求(List中的数据各自独立,对执行顺序无要求),考虑到可以使用多线程加快处理速度。

封装了如下方法:


        /// 
        /// 多线程处理数据-无返回值
        /// 
        /// 
        /// 数据处理方法
        /// 开启线程数量
        /// 是否等待全部结束
        static void RunTask(List list, Action action, int threadCount = 5, bool waitFlag = true)
        {
            var queue = new ConcurrentQueue(list);
            threadCount = Math.Min(threadCount, list.Count);
            Task[] tasks = new Task[threadCount];
            for (int i = 0; i < threadCount; i++)
            {
                tasks[i] = Task.Run(() =>
                {
                    while (queue.TryDequeue(out T t))
                    {
                        action(t);
                    }
                });
            }
            if (waitFlag)
            {
                Task.WaitAll(tasks);
            }
        }

        /// 
        /// 多线程处理数据-有返回值
        /// 
        /// 待处理数据
        /// 数据处理方法
        /// 开启线程数量
        static List RunTask(List list, Func func, int threadCount = 5)
        {
            var result = new ConcurrentBag();
            var queue = new ConcurrentQueue(list);
            threadCount = Math.Min(threadCount, list.Count);
            var tasks = new Task[threadCount];
            for (int i = 0; i < threadCount; i++)
            {
                tasks[i] = Task.Run(() =>
                {
                    while (queue.TryDequeue(out T t))
                    {
                        result.Add(func(t));
                    }
                });
            }
            Task.WaitAll(tasks);
            return result.ToList();
        }

使用示例:


        public void DoSingle(User user, string param1, string param2)
        {
            //HttpHelper.Get("http://xxxx/userid="+user.id);
            //doSomething();
            Console.WriteLine(user.Id + "-" + user.Name + "-" + param1 + "-" + param2);
        }
        public string DoSingle2(User user, string param1, string param2)
        {
            //HttpHelper.Get("http://xxxx/userid="+user.id);
            //doSomething();
            Console.WriteLine(user.Id + "-" + user.Name + "-" + param1 + "-" + param2);
            return user.Id + "-666";
        }


        public class User
        {
            /// 
            /// id
            /// 
            public int Id { get; set; }
            /// 
            /// 姓名
            /// 
            public string Name { get; set; }
        }

        public void Test()
        {
            var list = new List {
                new User { Id = 1, Name = "张三" },
                new User { Id = 2, Name = "李四" },
                new User { Id = 3, Name = "王五" }
            };
            RunTask(list, t => DoSingle(t, "参数1", "参数2"));
            var retList = RunTask(list, t => DoSingle2(t, "参数1", "参数2"));
        }

你可能感兴趣的:(C#,c#,list,多线程)