现在有这样一个问题,存在一个集合,集合中的元素很多,为了提高处理速度,于是使用下面的代码(多线程)来处理集合,思路是这样:使用M个多线程,将集合分成M页,每个线程处理该集合中第i页的数据,但是不知道是什么原因,不论我将M设为多少,整个处理时间都没有得到明显的改善,请高手指点
public class Program
{
public static void Main()
{
DateTime dt1 = DateTime.Now;
List<Num> list1 = new List<Num>();
for (int i = 0; i < 200000; i++)
{
list1.Add(new Num(100));
} int M = 5;
Thread[] threads = new Thread[M];
for (int i = 0; i < M; i++)
{
threads[i] = new Thread(new ParameterizedThreadStart(UpdateNumList));
threads[i].Start(new NumDecorate(list1, list1.Count / M, i));
threads[i].Join();
}
DateTime dt2 = DateTime.Now;
Console.WriteLine("{0}--{1}", dt1.ToLongTimeString(), dt2.ToLongTimeString());
Console.Read();
} public static void UpdateNumList(Object p)
{
NumDecorate nd = (NumDecorate)p;
for (int i = 0; i < nd.PageSize; i++)
{
nd.NumList[nd.PageIndex * nd.PageSize + i].Value += nd.PageIndex * nd.PageSize + i;
Console.WriteLine("{0}--{1}", nd.PageIndex * nd.PageSize + i, nd.NumList[nd.PageIndex * nd.PageSize + i].Value);
//Thread.Sleep(300);
}
}
} public class Num
{
public Num(int m)
{
this.Value = m;
} public int Value { get; set; }
} public class NumDecorate
{
public List<Num> NumList { get; set; }
public int PageSize { get; set; }
public int PageIndex { get; set; } public NumDecorate(List<Num> numList, int pageSize, int pageIndex)
{
this.NumList = numList;
this.PageIndex = pageIndex;
this.PageSize = pageSize;
}
}
public class Program
{
public static void Main()
{
DateTime dt1 = DateTime.Now;
List<Num> list1 = new List<Num>();
for (int i = 0; i < 200000; i++)
{
list1.Add(new Num(100));
} int M = 5;
Thread[] threads = new Thread[M];
for (int i = 0; i < M; i++)
{
threads[i] = new Thread(new ParameterizedThreadStart(UpdateNumList));
threads[i].Start(new NumDecorate(list1, list1.Count / M, i));
threads[i].Join();
}
DateTime dt2 = DateTime.Now;
Console.WriteLine("{0}--{1}", dt1.ToLongTimeString(), dt2.ToLongTimeString());
Console.Read();
} public static void UpdateNumList(Object p)
{
NumDecorate nd = (NumDecorate)p;
for (int i = 0; i < nd.PageSize; i++)
{
nd.NumList[nd.PageIndex * nd.PageSize + i].Value += nd.PageIndex * nd.PageSize + i;
Console.WriteLine("{0}--{1}", nd.PageIndex * nd.PageSize + i, nd.NumList[nd.PageIndex * nd.PageSize + i].Value);
//Thread.Sleep(300);
}
}
} public class Num
{
public Num(int m)
{
this.Value = m;
} public int Value { get; set; }
} public class NumDecorate
{
public List<Num> NumList { get; set; }
public int PageSize { get; set; }
public int PageIndex { get; set; } public NumDecorate(List<Num> numList, int pageSize, int pageIndex)
{
this.NumList = numList;
this.PageIndex = pageIndex;
this.PageSize = pageSize;
}
}
{
threads[i] = new Thread(new ParameterizedThreadStart(UpdateNumList));
threads[i].Start(new NumDecorate(list1, list1.Count / M, i));
//threads[i].Join();
}
for (int i = 0; i < M; i++)
{
threads[i].Join();
}
另外同意ls的观点,不是线程越多越快哈。
threads[i].Join();这样的话,当前线程没结束,下一个线程不会启动。
这和同步处理是一样的。
for (int i = 0; i < M; i++)
{
threads[i] = new Thread(new ParameterizedThreadStart(UpdateNumList));
threads[i].Start(new NumDecorate(list1, list1.Count / M, i));
threads[i].Join();
}
使用Join方法,建议楼主查看下Join方法的用法,你在这里Join,则各个线程是依次进行的,threads[1]处理完后,才会创建threads[2],依次进行下去。也就是说楼主的运算没有实现多线程的机制,而且比没有使用多线程处理速度更慢了
for (int i = 0; i < M; i++)
{
threads[i] = new Thread(new ParameterizedThreadStart(UpdateNumList));
threads[i].Start(new NumDecorate(list1, list1.Count / M, i));
threads[i].Join();
}
修改为
System.Threading.Tasks.Parallel.For(0, M, i =>
{
UpdateNumList(new NumDecorate(list1, list1.Count / M, i));
});这样对你的数据量来说可能提高只有几个毫秒,但是如果你将数量改为2000000,你可以看到很大的提升
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;namespace ThreadPoolExample
{
public class Program
{
public static void Main()
{
DateTime dt1 = DateTime.Now;
List<Num> list1 = new List<Num>();
//这里修改为了2000000
for (int i = 0; i < 2000000; i++)
{
list1.Add(new Num(100));
}
int M = 5; System.Threading.Tasks.Parallel.For(0, M, i =>
{
UpdateNumList(new NumDecorate(list1, list1.Count / M, i));
}); DateTime dt2 = DateTime.Now;
Console.WriteLine("总共运行{0}毫秒", (dt2 - dt1).TotalMilliseconds);
Console.Read();
} public static void UpdateNumList(Object p)
{
NumDecorate nd = (NumDecorate)p;
for (int i = 0; i < nd.PageSize; i++)
{
nd.NumList[nd.PageIndex * nd.PageSize + i].Value += nd.PageIndex * nd.PageSize + i;
}
}
} public class Num
{
public Num(int m)
{
this.Value = m;
} public int Value { get; set; }
} public class NumDecorate
{
public List<Num> NumList { get; set; }
public int PageSize { get; set; }
public int PageIndex { get; set; } public NumDecorate(List<Num> numList, int pageSize, int pageIndex)
{
this.NumList = numList;
this.PageIndex = pageIndex;
this.PageSize = pageSize;
}
}}
你可以试下和之前的运算速度比较(代码中我修改了数据量的大小),因为对小数据量进行并行处理没有实际意义
{
UpdateNumList(new NumDecorate(list1, list1.Count / M, i));
});