原文发布时间为:2008-11-26 —— 来源于本人的百度文章 [由搬家工具导入]
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace baidu
{
class Class8
{
//public void Function()
//{
// System.Object lockThis = new System.Object();
// lock (lockThis)
// {
// // Access thread-sensitive resources.
// }
//}
//static AutoResetEvent autoEvent;
//static void DoWork()
//{
// Console.WriteLine(" worker thread started, now waiting on event...");
// autoEvent.WaitOne();
// Console.WriteLine(" worker thread reactivated, now exiting...");
//}
//static void Main()
//{
// autoEvent = new AutoResetEvent(false);
// Console.WriteLine("main thread starting worker thread...");
// Thread t = new Thread(DoWork);
// t.Start();
// Console.WriteLine("main thrad sleeping for 1 second...");
// Thread.Sleep(1000);
// Console.WriteLine("main thread signaling worker thread...");
// autoEvent.Set();
// Console.ReadLine();
//}
//static void Main()
//{
// // Create the thread object. This does not start the thread.
// Worker workerObject = new Worker();
// Thread workerThread = new Thread(workerObject.DoWork);
// // Start the worker thread.
// workerThread.Start();
// Console.WriteLine("main thread: Starting worker thread...");
// // Loop until worker thread activates.
// while (!workerThread.IsAlive) ;
// // Put the main thread to sleep for 1 millisecond to
// // allow the worker thread to do some work:
// Thread.Sleep(1);
// // Request that the worker thread stop itself:
// workerObject.RequestStop();
// // Use the Join method to block the current thread
// // until the object's thread terminates.
// workerThread.Join();
// Console.WriteLine("main thread: Worker thread has terminated.");
// Console.ReadLine();
//}
static void Main()
{
const int FibonacciCalculations = 10;
// One event is used for each Fibonacci object
ManualResetEvent[] doneEvents = new ManualResetEvent[FibonacciCalculations];
Fibonacci[] fibArray = new Fibonacci[FibonacciCalculations];
Random r = new Random();
// Configure and launch threads using ThreadPool:
Console.WriteLine("launching {0} tasks...", FibonacciCalculations);
for (int i = 0; i < FibonacciCalculations; i++)
{
doneEvents[i] = new ManualResetEvent(false);
Fibonacci f = new Fibonacci(r.Next(20, 40), doneEvents[i]);
fibArray[i] = f;
ThreadPool.QueueUserWorkItem(f.ThreadPoolCallback, i);
}
// Wait for all threads in pool to calculation...
WaitHandle.WaitAll(doneEvents);
Console.WriteLine("All calculations are complete.");
// Display the results...
for (int i = 0; i < FibonacciCalculations; i++)
{
Fibonacci f = fibArray[i];
Console.WriteLine("Fibonacci({0}) = {1}", f.N, f.FibOfN);
}
Console.ReadLine();
}
}
public class Worker
{
// This method will be called when the thread is started.
public void DoWork()
{
while (!_shouldStop)
{
Console.WriteLine("worker thread: working...");
}
Console.WriteLine("worker thread: terminating gracefully.");
}
public void RequestStop()
{
_shouldStop = true;
}
// Volatile is used as hint to the compiler that this data
// member will be accessed by multiple threads.
private volatile bool _shouldStop;
}
public class Fibonacci
{
public Fibonacci(int n, ManualResetEvent doneEvent)
{
_n = n;
_doneEvent = doneEvent;
}
// Wrapper method for use with thread pool.
public void ThreadPoolCallback(Object threadContext)
{
int threadIndex = (int)threadContext;
Console.WriteLine("thread {0} started...", threadIndex);
_fibOfN = Calculate(_n);
Console.WriteLine("thread {0} result calculated...", threadIndex);
_doneEvent.Set();
}
// Recursive method that calculates the Nth Fibonacci number.
public int Calculate(int n)
{
if (n <= 1)
{
return n;
}
return Calculate(n - 1) + Calculate(n - 2);
}
public int N { get { return _n; } }
private int _n;
public int FibOfN { get { return _fibOfN; } }
private int _fibOfN;
private ManualResetEvent _doneEvent;
}
}