|
多线程使用System.Threading类
以下是一个例子
class Fibonacci
{
//share
public static int [] item; //read from it
public static int[] result; //write into it
//instance
public int N { get { return _n; } }
public int _start, _end;
private int _n;
public int FibOfN { get { return _fibOfN; } }
private int _fibOfN;
private ManualResetEvent _doneEvent;
public Fibonacci(int start, int end, ManualResetEvent doneEvent)
{
_start = start;
_end = end;
_doneEvent = doneEvent;
}
public Fibonacci(int start, int end)
{
_start = start;
_end = end;
}
// Wrapper method for use with thread pool.
public void ThreadPoolCallback(Object threadContext)
{
int threadIndex = (int)threadContext;
Calculate();
_doneEvent.Set();
}
// Recursive method that calculates the Nth Fibonacci number.
public void Calculate()
{
for (int i = _start; i < _end; i++)
{
int d = item[i];
for (int j = 0; j < 10000; j++) //注意,这里设置大值使单个线程对单个共享数据的运算时间较大
d += 0;
lock(this) //在本程序中可以不加,因为没有互斥
result[i] = d;
}
}
}
class TestThreading
{
public static void test()
{
Fibonacci.item = new int[10000];
Fibonacci.result = new int[10000];
for (int i = 0; i < 10000; i++)
{
Fibonacci.item[i] = i + 1;
}
Stopwatch watch = new Stopwatch();
watch.Start();
Fibonacci f = new Fibonacci(0, 10000);
f.Calculate();
watch.Stop();
Console.WriteLine("Time no thrading takes " + watch.Elapsed);
Console.ReadLine();
for (int i = 0; i < 10000; i++)
{
Console.Write(Fibonacci.result[i] + " ");
}
Console.ReadLine();
//thread 5
watch = new Stopwatch();
watch.Start();
Fibonacci[] fibarray = new Fibonacci[5];
ManualResetEvent[] doneEvents = new ManualResetEvent[5];
for (int i = 0; i < 5; i++)
{
int start = i * 2000;
int end = (i + 1) * 2000;
doneEvents[i] = new ManualResetEvent(false);
fibarray [i] = new Fibonacci (start ,end ,doneEvents[i]);
ThreadPool.QueueUserWorkItem(fibarray[i].ThreadPoolCallback, i);
}
WaitHandle.WaitAll(doneEvents);
watch.Stop();
Console.WriteLine("Time 5 threads takes " + watch.Elapsed);
Console.ReadLine();
for (int i = 0; i < 10000; i++)
{
Console.Write(Fibonacci.result[i] + " ");
}
Console.ReadLine();
}
}
需要注意的一点就是,如果多个线程访问同一个数据效率是没有提升反而因为线程初始化等开销加大
这就是为什么上面的J如果是小的数目,比如5,多线程效率反而不如单线程 |
|