线程池使用
(一)在线程池中调用委托
代码class Program { private delegate string RunOnThreadPool(out int threadId); static void Main(string[] args) { int threadId = 0; RunOnThreadPool poolDelegate = Test; var t = new Thread(() => Test(out threadId)); t.Start(); t.Join(); Console.WriteLine("Main: Thread id: {0}", threadId); IAsyncResult r = poolDelegate.BeginInvoke(out threadId, Callback, "a delegate asynchronous call"); r.AsyncWaitHandle.WaitOne(); //此处没程序也可以正常执行。不一定要等待 string result = poolDelegate.EndInvoke(out threadId, r); //得到Test()方法返回的结果。且此时终止了Test()执行,同时启动异步Callback()执行。 Console.WriteLine("Main: Thread pool worker thread id: {0}", threadId); Console.WriteLine(result); Thread.Sleep(TimeSpan.FromSeconds(2)); Console.WriteLine("Main: End......."); Console.ReadKey(); } private static void Callback(IAsyncResult ar) { Console.WriteLine("Callback: Starting a callback..."); Console.WriteLine("Callback: State passed to a callbak: {0}", ar.AsyncState); Console.WriteLine("Callback: Is thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread); Console.WriteLine("Callback: Thread pool worker thread id: {0}", Thread.CurrentThread.ManagedThreadId); } private static string Test(out int threadId) { Console.WriteLine("Test: Starting..."); Console.WriteLine("Test: Is thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread); Thread.Sleep(TimeSpan.FromSeconds(2)); threadId = Thread.CurrentThread.ManagedThreadId; return string.Format("Test: Thread pool worker thread id was: {0}", threadId); } }
代码分析(见下图)
(二)线程操作中的临界区问题
代码class Program { static void Main(string[] args) { Console.WriteLine("Incorrect counter"); var c = new Counter(); var t1 = new Thread(() => TestCounter(c)); var t2 = new Thread(() => TestCounter(c)); var t3 = new Thread(() => TestCounter(c)); t1.Start(); t2.Start(); t3.Start(); t1.Join(); t2.Join(); t3.Join(); Console.WriteLine("Total count: {0}", c.Count); Console.WriteLine("--------------------------"); Console.WriteLine("Correct counter"); var c1 = new CounterWithLock(); t1 = new Thread(() => TestCounter(c1)); t2 = new Thread(() => TestCounter(c1)); t3 = new Thread(() => TestCounter(c1)); t1.Start(); t2.Start(); t3.Start(); t1.Join(); t2.Join(); t3.Join(); Console.WriteLine("Total count: {0}", c1.Count); Console.ReadKey(); } static void TestCounter(CounterBase c) { Console.WriteLine("线程ID:" + Thread.CurrentThread.ManagedThreadId); for (int i = 0; i < 5; i++) { c.Increment(); c.Decrement(); } } class Counter : CounterBase { public int Count { get; private set; } public override void Increment() { Count++; Console.WriteLine("++ Count={0} 线程ID={1}", Count, Thread.CurrentThread.ManagedThreadId); } public override void Decrement() { Count--; Console.WriteLine("-- Count={0} 线程ID={1}", Count, Thread.CurrentThread.ManagedThreadId); } } class CounterWithLock : CounterBase { private readonly object _syncRoot = new Object(); public int Count { get; private set; } public override void Increment() { lock (_syncRoot) { Count++; Console.WriteLine("++ Count={0} 线程ID={1}", Count, Thread.CurrentThread.ManagedThreadId); } } public override void Decrement() { lock (_syncRoot) { Count--; Console.WriteLine("-- Count={0} 线程ID={1}", Count, Thread.CurrentThread.ManagedThreadId); } } } abstract class CounterBase { public abstract void Increment(); public abstract void Decrement(); } }