ImageVerifierCode 换一换
格式:DOCX , 页数:9 ,大小:17.30KB ,
资源ID:3804863      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/3804863.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(c#多线程编程笔记5完结.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

c#多线程编程笔记5完结.docx

1、c#多线程编程笔记5完结c#多线程编程笔记5(完结) a) 使用InterLocked类InterLocked使用于递增、递减以及更改变量值这类较为简单的操作。如果所有的任务都是在同步上下文中的一些简单操作,那么InterLocked类作为一个非常便捷的方法,可以大大减少需要编写的代码量。笔者没有在如下的试例程序中没有感觉到它的功用,当然 也不排除笔者技术有限未能理解到设计者的心思。例子8:using System;using System.Collections.Generic;using System.Text;using System.Threading;namespace Consol

2、eApplication1 public class InterLocked8 public static int i =0; public void Method1() if (i 20) Interlocked.Increment(ref i); else Interlocked.Decrement(ref i); Thread.Sleep(500); Console.WriteLine(Current thread is 0,the value of i is 1, Thread.CurrentThread.Name, i); class MainEntryPoint1 public s

3、tatic void Main() Thread myThread; InterLocked8 il8 = new InterLocked8(); for (int n = 0; n 20; n+) myThread = new Thread(new ThreadStart(il8.Method1); myThread.Name = String.Format(Thread0, n); myThread.Start(); Console.ReadLine(); b) 无等待读取当一个线程处在更改变量值的过程中,另一个线程也要更改该变量的值或需要读取变量值,就会出现同步的问题,前文中已介绍了这些

4、同步技术都用当一个线程执行受保护的代码部分时,就会阻塞其它线程对这部分的操作。但如果所有的线程都只读取这个资源,而不改变它的时候,这样做其实是没有必要与浪费时间的。在。NET中用无等待读取来提供这方面的功能。例子9:using System;using System.Collections.Generic;using System.Text;using System.Threading;namespace ConsoleApplication1 public class ReaderWriterClass protected ReaderWriterLock m_readerLock = ne

5、w ReaderWriterLock(); protected int m_counter = 0; protected int m_readerBlocks = 0; protected int m_writerBlocks = 0; protected void ThreadOneMethod() for (int i = 0; i 200; i+) try m_readerLock.AcquireReaderLock(0); try System.Console.WriteLine(m_counter); finally m_readerLock.ReleaseReaderLock();

6、 catch (Exception ) Interlocked.Increment(ref m_readerBlocks); protected void ThreadTwoMethod() for (int i = 0; i 100; i+) try m_readerLock.AcquireWriterLock(0); try Interlocked.Increment(ref m_counter); finally m_readerLock.ReleaseWriterLock(); catch (Exception) Interlocked.Increment(ref m_writerBl

7、ocks); Thread.Sleep(1); public int ReaderBlocks get return m_readerBlocks; public int WriteerBlocks get return m_writerBlocks; static void Main() ReaderWriterClass exampleClass = new ReaderWriterClass(); Thread threadOne = new Thread(new ThreadStart(exampleClass.ThreadOneMethod); Thread threadTwo=ne

8、w Thread(new ThreadStart(exampleClass.ThreadTwoMethod); threadOne.Start(); threadTwo.Start(); threadOne.Join(); threadTwo.Join(); System.Console.WriteLine(Reader Blocks 0,writer blocks 1,exampleClass.ReaderBlocks,exampleClass.WriteerBlocks); System.Console.Read(); 第四部分 对非同步线程使用线程池线程池是可以用来在后台执行多个任务的线

9、程集合。它的提出主要是因为有很多线程是在某一事件被触发之后才发生的,在这一事件发生之前这是处于休眠或者是等待状态,而在它的触发事件之后,它可以得到执行,执行完成以后,又进入休眠状态。a) WaitCallBackWaitCallBack表示线程池要执行的回调方法。语法如下:WaitCallBack MyCallBack=new WaitCallBack(ThreadPoolWorkerThreadMethod);它的语法与ThreadStart差不多,但它委托的事件有一参数,它委托的事件的申明如下:例子10:Static void ThreadPoolWorkerThreadMethod(Ob

10、jectstateObject).stateObject是一个状态对象,能够将信息传递给辅助线程。using System;using System.Collections.Generic;using System.Text;using System.Threading;namespace ConsoleApplication1 class Class1 public static void Main() ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc); Console.WriteLine(Main thread does s

11、ome work,then sleeps.); Thread.Sleep(1000); Console.WriteLine(Main Thread Exit.); Console.Read(); public static void ThreadProc(object stateInfo) Console.WriteLine(Hello from the thread pool!); b) 将工作项排入队列要使用线程池,就要先要调用ThreadPool.QueueUserWorkItem方法将工作项加入队列。语法:ThreadPool.QueueUserWorkItem(new WaitCal

12、lback(ThreadPoolThreadMethod);例程11:using System;using System.Collections.Generic;using System.Text;using System.Threading;namespace ConsoleApplication1 class QueueUserWorkerItem1 static void Main() Console.WriteLine(Main Thread Queing the work item.); ThreadPool.QueueUserWorkItem(new WaitCallback(Th

13、readPoolThreadMethod); Console.WriteLine(Main Thread Press the Enter key t exit the process.); Console.ReadLine(); Console.WriteLine(Main Thread Exiting the process.); static void ThreadPoolThreadMethod(Object stateObject) Console.WriteLine(Thread Pool Hello Thread Pool.); Console.ReadLine(); c) 向线程

14、传递数据ThreadPool构造函数使用一个WaitCallback委托作为参数,利用这个参数可以向ThreadPool传递任意状态或信息,从而传递给线程方法。例程12:using System;using System.Collections.Generic;using System.Text;using System.Threading;namespace ConsoleApplication1 class PassAWord10 static void Main() Console.WriteLine(Main Thread Queuing the work item.); Thread

15、Pool.QueueUserWorkItem(new WaitCallback(ThreadPoolThreadMethod),This is a state message); Console.WriteLine(Main Thread Press the Enter Key to exit the process.); Console.Read(); static void ThreadPoolThreadMethod(Object stateObject) Console.WriteLine(Thread Pool The data passed in is +stateObject.ToString()+);

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1