c#多线程编程笔记5(完结).doc_第1页
c#多线程编程笔记5(完结).doc_第2页
c#多线程编程笔记5(完结).doc_第3页
c#多线程编程笔记5(完结).doc_第4页
c#多线程编程笔记5(完结).doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

c#多线程编程笔记5(完结) a) 使用InterLocked类InterLocked使用于递增、递减以及更改变量值这类较为简单的操作。如果所有的任务都是在同步上下文中的一些简单操作,那么InterLocked类作为一个非常便捷的方法,可以大大减少需要编写的代码量。笔者没有在如下的试例程序中没有感觉到它的功用,当然 也不排除笔者技术有限未能理解到设计者的心思。例子8:using System;using System.Collections.Generic;using System.Text;using System.Threading;namespace ConsoleApplication1 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 static 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) 无等待读取当一个线程处在更改变量值的过程中,另一个线程也要更改该变量的值或需要读取变量值,就会出现同步的问题,前文中已介绍了这些同步技术都用当一个线程执行受保护的代码部分时,就会阻塞其它线程对这部分的操作。但如果所有的线程都只读取这个资源,而不改变它的时候,这样做其实是没有必要与浪费时间的。在。NET中用无等待读取来提供这方面的功能。例子9:using System;using System.Collections.Generic;using System.Text;using System.Threading;namespace ConsoleApplication1 public class ReaderWriterClass protected ReaderWriterLock m_readerLock = new 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(); 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_writerBlocks); 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=new 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(); 第四部分 对非同步线程使用线程池线程池是可以用来在后台执行多个任务的线程集合。它的提出主要是因为有很多线程是在某一事件被触发之后才发生的,在这一事件发生之前这是处于休眠或者是等待状态,而在它的触发事件之后,它可以得到执行,执行完成以后,又进入休眠状态。a) WaitCallBackWaitCallBack表示线程池要执行的回调方法。语法如下:WaitCallBack MyCallBack=new WaitCallBack(ThreadPoolWorkerThreadMethod);它的语法与ThreadStart差不多,但它委托的事件有一参数,它委托的事件的申明如下:例子10:Static void ThreadPoolWorkerThreadMethod(ObjectstateObject).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 some 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 WaitCallback(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(ThreadPoolThreadMethod); 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) 向线程传递数据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.); ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPoolThreadMethod),This is a state message)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论