急求多线程解决方法
假如我有1000000个数据,想用多线程处理(调用同一处理方法),目的是想提高处理能力.但本人写的测试发现处理速度并不比不用多线程快,请高手们帮我看看,照我的写法贴个代码出来看看
public static int n=20000;
public void begin()
{
Thread thr=new Thread(new ThreadStart(write));
thr.Start();
}
public void write()
{
Thread[] threadarray =new Thread[5];
for(int i=0;i<5;i++)
{
threadarray[i]=new Thread(new ThreadStart(testwrite));
threadarray[i].Name="ithread---"+i.ToString();
threadarray[i].IsBackground=true;
threadarray[i].Start();
}
for(int i=0;i<5;i++)
{
threadarray[i].Join();
}
}
public void testwrite()
{
while(n>0)
{
lock(typeof(bool))
{
if(n<0)
{
if(Thread.CurrentThread.IsAlive)
{
Thread.CurrentThread.Abort();
}
}
Console.WriteLine(n.ToString()+"***"+Thread.CurrentThread.Name.ToString());
Thread.Sleep(1);
n--;
}
}
}