让List实现并行插入(分块插入)
最近一直在学习多线程方面的知识。遇到一个需求,就是并行从数据库中获取数据,并且将所有线程得道的数据合并到一个集合中去。刚开始没有想到C#里面有支持并行的无锁集合,所以自己写了一个类,实现了对List集合的并行操作。第一次写博客,还望不要介意。希望前辈提提意见。具体类如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using Tool.DBManager; using Tool.Model; namespace Tool.Business { class CombineList { /// <summary> /// 存储单个List大小的数据 /// </summary> private string[] arryPart; /// <summary> /// 总List /// </summary> public static List< string> listString = new List< string>(); /// <summary> /// 存储线程信息 <当前线程id,当前数据大小> /// </summary> private static Dictionary< int, int> storeMes = new Dictionary< int, int>(); /// <summary> /// 线程id,也表示任务进行情况 /// </summary> private static int i = -1; /// <summary> /// 全局静态锁 /// </summary> private static object obj1 = new object(); //public static Dictionary<int, int> insertTime = new Dictionary<int, int>(); /// <summary> /// 构造函数 /// </summary> /// <param name="listLength"></param> public CombineList( int listLength) { arryPart = new string[listLength]; listString.AddRange(arryPart.ToList()); } /// <summary> /// 判断是否全部合并完成 /// </summary> /// <param name="count"></param> /// <returns></returns> public static bool IsSuccess( int count) { if (listString.Contains( null) || (i != count - 1)) { return false; } else return true; } /// <summary> /// 合并List /// </summary> /// <param name="obj"></param> public void BuildList( object obj) { List< string> threadList = (( ListMsg)obj).list; string fileName = (( ListMsg)obj).fileName; LogRecorder.PrintHeader( string.Format( "正在合并‘{0}’ List" , fileName)); int length = 0; Thread currentThread = Thread.CurrentThread; lock (obj1) { i++; length = threadList.Count; storeMes.Add(i, length); currentThread.Name = i.ToString(); } int start = 0; for ( int k = 0; k < Convert.ToInt32(currentThread.Name); k++) { start += storeMes[k]; } int num = Convert.ToInt32(currentThread.Name); LogRecorder.PrintHeader( string.Format( "当前线程:{0}" , num + 1)); LogRecorder.PrintHeader( string.Format( "start:{0}" , start)); LogRecorder.PrintHeader( string.Format( "num:{0}", storeMes[num])); for ( int j = start; j < start + storeMes[num]; j++) { listString[j] = threadList[j - start]; } LogRecorder.PrintHeader( string.Format( "List‘{0}’合并完成" , fileName)); } } }
类里的字段没有用属性表示,可以改为相应属性。
 
                    
                 
                
            
         
 浙公网安备 33010602011771号
浙公网安备 33010602011771号