




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、.net课程系列C#高级编程.NET方向 第二学期课程第六章 异步与多线程编程Newture新程教育本章目录引言引言进程与线程进程与线程进程与线程BeginInvoke和EndInvokeclass Program public delegate void PrintDelegate(string content); static void Main(string args) PrintDelegate printDelegate = Program.Print; IAsyncResult result = printDelegate.BeginInvoke(hello world, null
2、, null); printDelegate.EndInvoke(result); public static void Print(string content) Console.WriteLine(打印中n+content); System.Threading.Thread.Sleep(2000); IAsyncResult接口和AsyncResult类public interface IAsyncResult object AsyncState get; WaitHandle AsyncWaitHandle get; bool CompletedSynchronously get; bo
3、ol IsCompleted get; 属性返回类型说明IAsyncResult接口和AsyncResult类异步编程的4种方法 class Program public delegate void PrintDelegate(string content); static void Main(string args) int threadId = Thread.CurrentThread.ManagedThreadId; PrintDelegate printDelegate = Program.Print; Console.WriteLine(主线程Id:+threadId+t+打印方法开
4、始调用); IAsyncResult result = printDelegate.BeginInvoke(hello world, null, null); printDelegate.EndInvoke(result); public static void Print(string content) int threadId = Thread.CurrentThread.ManagedThreadId; Console.WriteLine(当前线程Id:+threadId); Console.WriteLine(打印中n+content); Thread.Sleep(2000); Con
5、sole.WriteLine(当前线程Id: + threadId+t+打印方法调用完毕); 异步编程的4种方法异步编程的4种方法 class Program public delegate void PrintDelegate(string content); static void Main(string args) int threadId = Thread.CurrentThread.ManagedThreadId; PrintDelegate printDelegate = Program.Print; Console.WriteLine(主线程Id:+threadId+t+打印方法
6、开始调用); IAsyncResult result = printDelegate.BeginInvoke(hello world, null, null); result.AsyncWaitHandle.WaitOne(5000, false); 异步编程的4种方法 class Program public delegate void PrintDelegate(string content); static void Main(string args) int threadId = Thread.CurrentThread.ManagedThreadId; PrintDelegate p
7、rintDelegate = Program.Print; Console.WriteLine(主线程Id:+threadId+t+打印方法开始调用); IAsyncResult result = printDelegate.BeginInvoke(hello world, null, null); while (!result.IsCompleted) Console.WriteLine(.); Thread.Sleep(500); 异步编程的4种方法异步编程的4种方法class Program public delegate void PrintDelegate(string conten
8、t); static void Main(string args) int threadId = Thread.CurrentThread.ManagedThreadId; PrintDelegate printDelegate = Program.Print; Console.WriteLine(主线程Id: + threadId + t + 打印方法开始调用); IAsyncResult result = printDelegate.BeginInvoke(hello world, PrintComplete, printDelegate); Thread.Sleep(10000); pu
9、blic static void PrintComplete(IAsyncResult result) int threadId = Thread.CurrentThread.ManagedThreadId; (result.AsyncState as PrintDelegate).EndInvoke(result); Console.WriteLine(当前线程Id: + threadId + t + 打印方法调用完毕) 多线程编程Thread类Thread类Thread类 class Program static void Main(string args) Console.WriteLi
10、ne(主线程:0,Thread.CurrentThread.ManagedThreadId); Thread thread1 = new Thread(Print); thread1.Start(); Thread thread2 = new Thread(PrintEx); thread2.Start(测试打印); public static void Print() int threadId = Thread.CurrentThread.ManagedThreadId; Console.WriteLine(当前线程id:0t1, threadId, DateTime.Now.ToShort
11、TimeString(); public static void PrintEx(object content) int threadId = Thread.CurrentThread.ManagedThreadId; Console.WriteLine(当前线程id:0t1, threadId, content); Thread类Thread类Thread类使用线程池使用线程池使用线程池使用线程池class Program static void Main(string args) ThreadPool.QueueUserWorkItem(Counter); ThreadPool.Queue
12、UserWorkItem(Counter,test); Console.WriteLine(线程ID=0主线程启动, Thread.CurrentThread.ManagedThreadId); Thread.Sleep(500); Console.WriteLine(主线程退出); private static void Counter(object state) Console.WriteLine(开始计数,从 1 到 100:); Console.WriteLine(传入的参数值为:0,state); for (int counter = 0; counter 100; counter+
13、) if (null = state)Console.WriteLine(线程ID=0 1,Thread.CurrentThread.ManagedThreadId,counter); elseConsole.WriteLine(线程ID=0 1, Thread.CurrentThread.ManagedThreadId, state);Thread.Sleep(100); 线程同步 class Program int count; public int Max get; set; public void Increase() for (int i = 0; i Max; i+) count+
14、; static void Main(string args) Thread threads = new Thread500; Program p = new Program() Max=10000 ; for (int i = 0; i threads.Length; i+) threadsi = new Thread(p.Increase); threadsi.Start(); for (int i = 0; i threads.Length; i+) threadsi.Join(); Console.WriteLine(0:N0, p.count); 线程同步 public void I
15、ncrease() for (int i = 0; i Max; i+) lock (this) count+; private object _lock = new object(); public void Increase() for (int i = 0; i Max; i+) lock (_lock) count+; 线程同步 private object _lock = new object(); public void Increase() for (int i = 0; i Max; i+) Monitor.Enter(_lock); try count+; finally M
16、onitor.Exit(_lock); 死锁public void Lock1() Console.WriteLine(线程0启动,执行Lock1(),Thread.CurrentThread.ManagedThreadId); lock (_lock_1) Thread.Sleep(1000); lock (_lock_2) Console.WriteLine(线程0从Lock1()退出,Thread.CurrentThread.ManagedThreadId);public void Lock2() Console.WriteLine(线程0启动,执行Lock2(), Thread.CurrentThread.ManagedThreadId); lock (_lock_2) Thread.Sleep(2000); lock (_lock_1) Console.WriteLine(线程0从Lock2()退出, Thr
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 商品鸽棚转让合同协议
- 快递合作协议合同书
- 2025商业购房合同模板
- 2025年无固定期限劳动合同与范本
- 2025标准供货合同协议书
- 2025大理石材供货合同范本
- 2025年人民币典当借款合同
- 咨询酒水合同协议
- 品牌经营权转让合同协议
- 2025汽车销售合同书范本标准范文
- Unit5SectionB2a-2e阅读课件人教版九年级英语全册
- 小儿肺炎的护理
- 航道整治试卷A
- 腹腔镜胃癌根治术护理教学查房
- 幼儿园优质公开课:中班科学《奇妙的紫甘蓝汁》课件
- 地球物理勘探-第三章磁法勘探1
- 卡压不锈钢管的施工组织方案
- 2022山东大学出版社校园招聘16人上岸笔试历年难、易错点考题附带参考答案与详解
- Rexroth (博世力士乐)VFC 3610系列变频器使用说明书
- DB32/T 4454-2023智慧化工园区建设规范
- 插齿机操作规程
评论
0/150
提交评论