.NET多线程执行函数

[来源] 达内    [编辑] 达内   [时间]2012-11-12

原因是DebugLZQ在写一个LINQ综合Demo的时候遇到了多线

前面几篇文章一直在写LINQ,这里为什么会出现多线程?原因是Deb ugLZQ在写一个LINQ综合Demo的时候遇到了多线程,便停下手来整理一下。关于多线程的文章,园子里很多很多,因此关于多线程理论性的东西,LZ就不去多说了,这篇博文主要是用最简单的例子,总结下多线程调用函数的相关注意点,重点偏向应用和记忆。

< h2 style="margin: 15px auto 2px; padding: 0px; font-size: 18px; font-weight: bold; color: rgb(85, 85, 85); font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(245, 248, 250); "> 1.多线程调 用无参函数                                                                                    < div style="margin: 5px 0px; padding: 5px; background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(85, 85, 85); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">
using
 System; using
 System.Collections.Generic; 
using

 System.Linq; using
 System.Text; using
 System.Threading;  namespace
 多线程 {     class
 Program     {         static
 void Main(string
[] args)         {             Console.WriteLine(
"

主线程开始"
);             Thread t = new
 Thread(new ThreadStart(ShowTime));
//

注意ThreadStart委托的定义形式

            t.Start();//
线程开始,控制权返回Main线程

            Console.WriteLine("
主线程继续执行"
);             //
while (t.IsAlive == true) ;

            Thread.Sleep(1000
);             t.Abort();             t.Join();//
阻塞Main线程,直到t终止

            Console.WriteLine("
--------------"
);             Console.ReadKey();         }         static
 void
 ShowTime()         {             

while (true
)             {                 Console.WriteLine(DateTime.Now.ToString());                            }         }     } }
< p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(85, 85, 85); font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; orphans: 2; text-align: left; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(245, 248, 250); ">注意ThreadStart委托的定义如下:

< p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(85, 85, 85); font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; orphans: 2; text-align: left; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(245, 248, 250); ">

< p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(85, 85, 85); font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; orphans: 2; text-align: left; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(245, 248, 250); "> 可见其对传递进来的函数要求是:返回值void,无参数。

< h2 style="margin: 15px auto 2px; padding: 0px; font-size: 18px; font-weight: bold; color: rgb(85, 85, 85); font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(245, 248, 250); "> 2.多线程调 用带参函数(两种方法)                                                                < div style="margin: 5px 0px; padding: 5px; background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(85, 85, 85); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">
using
 System; using
 System.Collections.Generic; 
using

 System.Linq; using
 System.Text; using
 System.Threading;  namespace
 多线程2_带参数 {     
class

 Program     {         static
 void Main(string
[] args)         {             Console.WriteLine(
"

Main线程开始"
);             Thread t = new
 Thread(new
 ParameterizedThreadStart(DoSomething));//

注意ParameterizedThreadStart委托的定义形式

            t.Start(new string


[]{"Hello
","
World"
});             Console.WriteLine(
"

Main线程继续执行"
);              Thread.Sleep(
1000

);             t.Abort();             t.Join();
//

阻塞Main线程,直到t终止

            Console.ReadKey();         }         
static void
 DoSomething(object
  s)         {             

string[] strs = s as
 

string[];             
while

 (true
)             {                 Console.WriteLine("


{0}--{1}",strs[0


],strs[1
]);             }         }     } }
< p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(85, 85, 85); font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; orphans: 2; text-align: left; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(245, 248, 250); ">注意ParameterizedThreadStart委托的定义如下:

< p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(85, 85, 85); font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; orphans: 2; text-align: left; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(245, 248, 250); ">

< p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(85, 85, 85); font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; orphans: 2; text-align: left; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(245, 248, 250); "> 可见其对传入函数的要求是:返回值void,参数个数1,参数类型object

< div style="margin: 5px 0px; padding: 5px; background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(85, 85, 85); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">
using
 System; using
 System.Collections.Generic; 
using

 System.Linq; using
 System.Text; using
 System.Threading;  namespace
 多线程2_带参数2 {        
class

 Program     {         static
 void Main(string
[] args)         {             Guest guest 
= new

 Guest()              {              Name
="

Hello"
, Age=99
             };             Thread t 

= new Thread(new
 ThreadStart(guest.DoSomething));//
注意ThreadStart委托的定义形式


            t.Start();              Thread.Sleep(
1000
);             t.Abort();             t.Join();

//
阻塞Main线程,直到t终止


            Console.ReadKey();         }     }     
//     class


 Guest     {         public 
string Name { get; set
; }         public
 int Age { get
; set; }          
public void
 DoSomething()         {             
while

 (true
)             {                 Console.WriteLine(

"{0}--{1}
"
, Name, Age);             }         }     } }
< p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(85, 85, 85); font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; orphans: 2; text-align: left; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(245, 248, 250); ">这个还是使用ThreadStart委托,对方法进行了一个封装。

< p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(85, 85, 85); font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; orphans: 2; text-align: left; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(245, 248, 250); ">两种方法,可随意选择,第一种貌似简洁一点。

< h2 style="margin: 15px auto 2px; padding: 0px; font-size: 18px; font-weight: bold; color: rgb(85, 85, 85); font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(245, 248, 250); "> 3.线程同步                                                                                                < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(85, 85, 85); font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; orphans: 2; text-align: left; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(245, 248, 250); "> 线程同步的方法有很多很多种volatile、Lock、InterLock、Monitor、Mutex、ReadWriteLock...

< p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(85, 85, 85); font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; orphans: 2; text-align: left; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(245, 248, 250); ">这里用lock说明问题:在哪里同步,用什么同步,同步谁?

< p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(85, 85, 85); font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; orphans: 2; text-align: left; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(245, 248, 250); ">首先感受下不同步会出现的问题:

< p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(85, 85, 85); font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; orphans: 2; text-align: left; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(245, 248, 250); ">

< p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(85, 85, 85); font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; orphans: 2; text-align: left; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(245, 248, 250); ">代码就是下面的代码去掉lock块。

< div style="margin: 5px 0px; padding: 5px; background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(85, 85, 85); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">
复制代码
using
 System; using
 System.Collections.Generic; 
using

 System.Linq; using
 System.Text; using
 System.Threading;  namespace
 多线程3_同步2 {     class
 Program     {         static
 object obj = new
 object();//
同步用


        static int
 balance = 500;          
static void
 Main(string
[] args)         {             Thread t1 

= new Thread(new
 ThreadStart(Credit));             t1.Start();              Thread t2 
= new Thread(new


 ThreadStart(Debit));             t2.Start();              Console.ReadKey();         }          static
 void
 Credit()         {             

for (int
 i = 0; i < 15
; i++)             {                 
lock

 (obj)                 {                     balance 
+= 100

;                     Console.WriteLine(
"

After crediting,balance is {0}
"

, balance);                 }             }         }         
static

 void
 Debit()         {             for
 (int

 i = 0; i < 15
; i++)             {                 
lock

 (obj)                 {                     balance 
-= 

100;                     Console.WriteLine(
"
After debiting,balance is {0}

"
, balance);                 }             }         }     } }
复制代码
< p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(85, 85, 85); font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; orphans: 2; text-align: left; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(245, 248, 250); "> 小结:多线程调用函数就是这样。在Winform中,控件绑定到特定的线程,从另一个线程更新控件,不应该直接调用该控件的成员,这个非常有用,下篇博文讲。

资源下载