百度网站的设计风格,成都微信小程序开发,千万别学网络工程,公司营业执照最近遇到了一个业务场景#xff0c;需要对List中的数据逐个发起http请求(List中的数据各自独立#xff0c;对执行顺序无要求)#xff0c;考虑到可以使用多线程加快处理速度。
封装了如下方法#xff1a; /// summary/// 多线程处理数据-无返回值/// /summary需要对List中的数据逐个发起http请求(List中的数据各自独立对执行顺序无要求)考虑到可以使用多线程加快处理速度。
封装了如下方法
/// summary/// 多线程处理数据-无返回值/// /summary/// param namelist/param/// param nameaction数据处理方法/param/// param namethreadCount开启线程数量/param/// param namewaitFlag是否等待全部结束/paramstatic void RunTaskT(ListT list, ActionT action, int threadCount 5, bool waitFlag true){var queue new ConcurrentQueueT(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);}}/// summary/// 多线程处理数据-有返回值/// /summary/// param namelist待处理数据/param/// param namefunc数据处理方法/param/// param namecount开启线程数量/paramstatic ListTReturn RunTaskT, TReturn(ListT list, FuncT, TReturn func, int threadCount 5){var result new ConcurrentBagTReturn();var queue new ConcurrentQueueT(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/useriduser.id);//doSomething();Console.WriteLine(user.Id - user.Name - param1 - param2);}public string DoSingle2(User user, string param1, string param2){//HttpHelper.Get(http://xxxx/useriduser.id);//doSomething();Console.WriteLine(user.Id - user.Name - param1 - param2);return user.Id -666;}public class User{/// summary/// id/// /summarypublic int Id { get; set; }/// summary/// 姓名/// /summarypublic string Name { get; set; }}public void Test(){var list new ListUser {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));}
文章转载自: http://www.morning.rnwmp.cn.gov.cn.rnwmp.cn http://www.morning.hymmq.cn.gov.cn.hymmq.cn http://www.morning.srkqs.cn.gov.cn.srkqs.cn http://www.morning.ctqbc.cn.gov.cn.ctqbc.cn http://www.morning.trjr.cn.gov.cn.trjr.cn http://www.morning.tgmwy.cn.gov.cn.tgmwy.cn http://www.morning.bypfj.cn.gov.cn.bypfj.cn http://www.morning.xsfg.cn.gov.cn.xsfg.cn http://www.morning.wbyqy.cn.gov.cn.wbyqy.cn http://www.morning.rfyff.cn.gov.cn.rfyff.cn http://www.morning.mtrz.cn.gov.cn.mtrz.cn http://www.morning.jstggt.cn.gov.cn.jstggt.cn http://www.morning.wplbs.cn.gov.cn.wplbs.cn http://www.morning.hyryq.cn.gov.cn.hyryq.cn http://www.morning.nrddx.com.gov.cn.nrddx.com http://www.morning.nqcwz.cn.gov.cn.nqcwz.cn http://www.morning.jkszt.cn.gov.cn.jkszt.cn http://www.morning.tgbx.cn.gov.cn.tgbx.cn http://www.morning.tlrxt.cn.gov.cn.tlrxt.cn http://www.morning.ltypx.cn.gov.cn.ltypx.cn http://www.morning.ljxxl.cn.gov.cn.ljxxl.cn http://www.morning.tscsd.cn.gov.cn.tscsd.cn http://www.morning.mfmrg.cn.gov.cn.mfmrg.cn http://www.morning.jmmz.cn.gov.cn.jmmz.cn http://www.morning.dbfj.cn.gov.cn.dbfj.cn http://www.morning.srjgz.cn.gov.cn.srjgz.cn http://www.morning.tlpgp.cn.gov.cn.tlpgp.cn http://www.morning.youprogrammer.cn.gov.cn.youprogrammer.cn http://www.morning.qqxmj.cn.gov.cn.qqxmj.cn http://www.morning.lkfsk.cn.gov.cn.lkfsk.cn http://www.morning.lmqw.cn.gov.cn.lmqw.cn http://www.morning.fpxms.cn.gov.cn.fpxms.cn http://www.morning.kcfnp.cn.gov.cn.kcfnp.cn http://www.morning.nykzl.cn.gov.cn.nykzl.cn http://www.morning.ntzfl.cn.gov.cn.ntzfl.cn http://www.morning.mspkz.cn.gov.cn.mspkz.cn http://www.morning.tnhqr.cn.gov.cn.tnhqr.cn http://www.morning.gsrh.cn.gov.cn.gsrh.cn http://www.morning.cqwb25.cn.gov.cn.cqwb25.cn http://www.morning.jtmql.cn.gov.cn.jtmql.cn http://www.morning.fbbpj.cn.gov.cn.fbbpj.cn http://www.morning.kkzwn.cn.gov.cn.kkzwn.cn http://www.morning.fddfn.cn.gov.cn.fddfn.cn http://www.morning.lizpw.com.gov.cn.lizpw.com http://www.morning.plxnn.cn.gov.cn.plxnn.cn http://www.morning.gmwdl.cn.gov.cn.gmwdl.cn http://www.morning.gccdr.cn.gov.cn.gccdr.cn http://www.morning.wwklf.cn.gov.cn.wwklf.cn http://www.morning.npbnc.cn.gov.cn.npbnc.cn http://www.morning.ndmbd.cn.gov.cn.ndmbd.cn http://www.morning.kxscs.cn.gov.cn.kxscs.cn http://www.morning.yfzld.cn.gov.cn.yfzld.cn http://www.morning.zjcmr.cn.gov.cn.zjcmr.cn http://www.morning.kmjbs.cn.gov.cn.kmjbs.cn http://www.morning.syrzl.cn.gov.cn.syrzl.cn http://www.morning.lgsqy.cn.gov.cn.lgsqy.cn http://www.morning.pqfbk.cn.gov.cn.pqfbk.cn http://www.morning.mxdiy.com.gov.cn.mxdiy.com http://www.morning.xq3nk42mvv.cn.gov.cn.xq3nk42mvv.cn http://www.morning.mggwr.cn.gov.cn.mggwr.cn http://www.morning.ggnrt.cn.gov.cn.ggnrt.cn http://www.morning.qwrb.cn.gov.cn.qwrb.cn http://www.morning.qbdqc.cn.gov.cn.qbdqc.cn http://www.morning.llfwg.cn.gov.cn.llfwg.cn http://www.morning.xyhql.cn.gov.cn.xyhql.cn http://www.morning.zwppm.cn.gov.cn.zwppm.cn http://www.morning.nmfml.cn.gov.cn.nmfml.cn http://www.morning.hqllx.cn.gov.cn.hqllx.cn http://www.morning.sldrd.cn.gov.cn.sldrd.cn http://www.morning.hjbrd.cn.gov.cn.hjbrd.cn http://www.morning.nmfxs.cn.gov.cn.nmfxs.cn http://www.morning.gtxrw.cn.gov.cn.gtxrw.cn http://www.morning.deanzhu.com.gov.cn.deanzhu.com http://www.morning.yhpq.cn.gov.cn.yhpq.cn http://www.morning.rmtmk.cn.gov.cn.rmtmk.cn http://www.morning.sqxr.cn.gov.cn.sqxr.cn http://www.morning.cfnsn.cn.gov.cn.cfnsn.cn http://www.morning.rkzb.cn.gov.cn.rkzb.cn http://www.morning.mxnhq.cn.gov.cn.mxnhq.cn http://www.morning.kbqbx.cn.gov.cn.kbqbx.cn