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

上传人:b****4 文档编号:3804863 上传时间:2022-11-25 格式:DOCX 页数:9 大小:17.30KB
下载 相关 举报
c#多线程编程笔记5完结.docx_第1页
第1页 / 共9页
c#多线程编程笔记5完结.docx_第2页
第2页 / 共9页
c#多线程编程笔记5完结.docx_第3页
第3页 / 共9页
c#多线程编程笔记5完结.docx_第4页
第4页 / 共9页
c#多线程编程笔记5完结.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

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

《c#多线程编程笔记5完结.docx》由会员分享,可在线阅读,更多相关《c#多线程编程笔记5完结.docx(9页珍藏版)》请在冰豆网上搜索。

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

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

 c#多线程编程笔记5(完结)

a)       使用InterLocked类

InterLocked使用于递增、递减以及更改变量值这类较为简单的操作。

如果所有的任务都是在同步上下文中的一些简单操作,那么InterLocked类作为一个非常便捷的方法,可以大大减少需要编写的代码量。

笔者没有在如下的试例程序中没有感觉到它的功用,当然也不排除笔者技术有限未能理解到设计者的心思。

例子8:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Threading;

 

namespaceConsoleApplication1

{

publicclassInterLocked8

{

publicstaticinti=0;

 

publicvoidMethod1()

{

if(i<20)

{

Interlocked.Increment(refi);

}

else

{

Interlocked.Decrement(refi);

}

Thread.Sleep(500);

Console.WriteLine("Currentthreadis{0},thevalueofiis{1}",Thread.CurrentThread.Name,i);

}

}

classMainEntryPoint1

{

publicstaticvoidMain()

{

ThreadmyThread;

InterLocked8il8=newInterLocked8();

for(intn=0;n<20;n++)

{

myThread=newThread(newThreadStart(il8.Method1));

myThread.Name=String.Format("Thread{0}",n);

myThread.Start();

}

Console.ReadLine();

}

}

}

b)      无等待读取

当一个线程处在更改变量值的过程中,另一个线程也要更改该变量的值或需要读取变量值,就会出现同步的问题,前文中已介绍了这些同步技术都用当一个线程执行受保护的代码部分时,就会阻塞其它线程对这部分的操作。

但如果所有的线程都只读取这个资源,而不改变它的时候,这样做其实是没有必要与浪费时间的。

在。

NET中用无等待读取来提供这方面的功能。

例子9:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Threading;

namespaceConsoleApplication1

{

publicclassReaderWriterClass

{

protectedReaderWriterLockm_readerLock=newReaderWriterLock();

protectedintm_counter=0;

protectedintm_readerBlocks=0;

protectedintm_writerBlocks=0;

 

protectedvoidThreadOneMethod()

{

for(inti=0;i<200;i++)

{

try

{

m_readerLock.AcquireReaderLock(0);

try

{

System.Console.WriteLine(m_counter);

}

finally

{

m_readerLock.ReleaseReaderLock();

}

}

catch(Exception)

{

Interlocked.Increment(refm_readerBlocks);

}

}

}

protectedvoidThreadTwoMethod()

{

for(inti=0;i<100;i++)

{

try

{

m_readerLock.AcquireWriterLock(0);

try

{

Interlocked.Increment(refm_counter);

}

finally

{

m_readerLock.ReleaseWriterLock();

}

}

catch(Exception)

{

Interlocked.Increment(refm_writerBlocks);

}

Thread.Sleep

(1);

}

}

 

publicintReaderBlocks

{

get

{

returnm_readerBlocks;

}

}

 

publicintWriteerBlocks

{

get

{

returnm_writerBlocks;

}

}

 

staticvoidMain()

{

ReaderWriterClassexampleClass=newReaderWriterClass();

ThreadthreadOne=newThread(newThreadStart(exampleClass.ThreadOneMethod));

ThreadthreadTwo=newThread(newThreadStart(exampleClass.ThreadTwoMethod));

 

threadOne.Start();

threadTwo.Start();

 

threadOne.Join();

threadTwo.Join();

 

System.Console.WriteLine("ReaderBlocks{0},writerblocks{1}",exampleClass.ReaderBlocks,exampleClass.WriteerBlocks);

 

System.Console.Read();

}

}

}

第四部分对非同步线程使用线程池

线程池是可以用来在后台执行多个任务的线程集合。

它的提出主要是因为有很多线程是在某一事件被触发之后才发生的,在这一事件发生之前这是处于休眠或者是等待状态,而在它的触发事件之后,它可以得到执行,执行完成以后,又进入休眠状态。

a)       WaitCallBack

WaitCallBack表示线程池要执行的回调方法。

语法如下:

WaitCallBack[MyCallBack]=newWaitCallBack([ThreadPoolWorkerThreadMethod]);

它的语法与ThreadStart差不多,但它委托的事件有一参数,它委托的事件的申明如下:

例子10:

StaticvoidThreadPoolWorkerThreadMethod(Object[stateObject])

{

….

}

[stateObject]是一个状态对象,能够将信息传递给辅助线程。

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Threading;

 

namespaceConsoleApplication1

{

classClass1

{

publicstaticvoidMain()

{

ThreadPool.QueueUserWorkItem(newWaitCallback(ThreadProc));

 

Console.WriteLine("Mainthreaddoessomework,thensleeps.");

 

Thread.Sleep(1000);

 

Console.WriteLine("MainThreadExit.");

Console.Read();

}

publicstaticvoidThreadProc(objectstateInfo)

{

Console.WriteLine("Hellofromthethreadpool!

");

}

}

}

 

b)      将工作项排入队列

要使用线程池,就要先要调用ThreadPool.QueueUserWorkItem方法将工作项加入队列。

语法:

ThreadPool.QueueUserWorkItem(newWaitCallback(ThreadPoolThreadMethod));

例程11:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Threading;

 

namespaceConsoleApplication1

{

classQueueUserWorkerItem1

{

staticvoidMain()

{

Console.WriteLine("{MainThread}Queingtheworkitem.");

ThreadPool.QueueUserWorkItem(newWaitCallback(ThreadPoolThreadMethod));

 

Console.WriteLine("{MainThread}Pressthe'Enter'keytexittheprocess.");

Console.ReadLine();

Console.WriteLine("{MainThread}Exitingtheprocess.");

}

staticvoidThreadPoolThreadMethod(ObjectstateObject)

{

Console.WriteLine("{ThreadPool}HelloThreadPool.");

Console.ReadLine();

}

}

}

c)       向线程传递数据

ThreadPool构造函数使用一个WaitCallback委托作为参数,利用这个参数可以向ThreadPool传递任意状态或信息,从而传递给线程方法。

例程12:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Threading;

 

namespaceConsoleApplication1

{

classPassAWord10

{

staticvoidMain()

{

Console.WriteLine("{MainThread}Queuingtheworkitem.");

ThreadPool.QueueUserWorkItem(newWaitCallback(ThreadPoolThreadMethod),"Thisisastatemessage");

 

Console.WriteLine("{MainThread}Pressthe'Enter'Keytoexittheprocess.");

Console.Read();

}

staticvoidThreadPoolThreadMethod(ObjectstateObject)

{

Console.WriteLine("{ThreadPool}Thedatapassedinis'"+stateObject.ToString()+"'");

}

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 入党转正申请

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

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