版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 喷水器产业链招商引资的调研报告
- 药用锭剂项目运营指导方案
- 增白霜产品供应链分析
- 区块链金融市场交易行业市场调研分析报告
- 企业公益慈善活动创意策划与执行服务行业营销策略方案
- 厨房用具产品供应链分析
- 书法服务行业市场调研分析报告
- 事故信号发射器产品供应链分析
- 仿皮包产品供应链分析
- 矿泉水盐项目营销计划书
- 部编小学语文单元作业设计六年级上册第八单元 8
- 文明礼貌教育主题班会内容
- 浅议安全和质量和进度之间关系
- 小学三年级数独比赛“六宫”练习题(88道)
- 视光门诊运营计划方案
- 护理评估单工具的使用
- 2021年大庆精神铁人精神知识竞赛题库
- 浙江泰隆商业银行员工手册
- 审计技能实训教程(喻竹 第二版) 教案全套 1.1-9.2 业务承接与评价-审计底稿归档
- 郑州热力公司招聘题库答案
- 徐州市2023-2024学年八年级上学期期末数学试卷(含答案解析)
评论
0/150
提交评论