实例讲解.NET多线程执行函数

开发 后端
这篇博文主要是用最简单的例子,总结下多线程调用函数的相关注意点,重点偏向应用和记忆。

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

1.多线程调用无参函数                                                                                   

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. using System.Threading; 
  6.  
  7. namespace 多线程 
  8.     class Program 
  9.     { 
  10.         static void Main(string[] args) 
  11.         { 
  12.             Console.WriteLine("主线程开始"); 
  13.             Thread t = new Thread(new ThreadStart(ShowTime));//注意ThreadStart委托的定义形式 
  14.             t.Start();//线程开始,控制权返回Main线程 
  15.             Console.WriteLine("主线程继续执行"); 
  16.             //while (t.IsAlive == true) ; 
  17.             Thread.Sleep(1000); 
  18.             t.Abort(); 
  19.             t.Join();//阻塞Main线程,直到t终止 
  20.             Console.WriteLine("--------------"); 
  21.             Console.ReadKey(); 
  22.         } 
  23.         static void ShowTime() 
  24.         { 
  25.             while (true
  26.             { 
  27.                 Console.WriteLine(DateTime.Now.ToString());                
  28.             } 
  29.         } 
  30.     } 

注意ThreadStart委托的定义如下:

可见其对传递进来的函数要求是:返回值void,无参数。

2.多线程调用带参函数(两种方法)     

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. using System.Threading; 
  6.  
  7. namespace 多线程2_带参数 
  8.     class Program 
  9.     { 
  10.         static void Main(string[] args) 
  11.         { 
  12.             Console.WriteLine("Main线程开始"); 
  13.             Thread t = new Thread(new ParameterizedThreadStart(DoSomething));//注意ParameterizedThreadStart委托的定义形式 
  14.             t.Start(new string[]{"Hello","World"}); 
  15.             Console.WriteLine("Main线程继续执行"); 
  16.  
  17.             Thread.Sleep(1000); 
  18.             t.Abort(); 
  19.             t.Join();//阻塞Main线程,直到t终止 
  20.             Console.ReadKey(); 
  21.         } 
  22.         static void DoSomething(object  s) 
  23.         { 
  24.             string[] strs = s as string[]; 
  25.             while (true
  26.             { 
  27.                 Console.WriteLine("{0}--{1}",strs[0],strs[1]); 
  28.             } 
  29.         } 
  30.     } 
注意ParameterizedThreadStart委托的定义如下:

可见其对传入函数的要求是:返回值void,参数个数1,参数类型object

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. using System.Threading; 
  6.  
  7. namespace 多线程2_带参数2 
  8. {    
  9.     class Program 
  10.     { 
  11.         static void Main(string[] args) 
  12.         { 
  13.             Guest guest = new Guest()  
  14.             { 
  15.              Name="Hello", Age=99 
  16.             }; 
  17.             Thread t = new Thread(new ThreadStart(guest.DoSomething));//注意ThreadStart委托的定义形式 
  18.             t.Start(); 
  19.  
  20.             Thread.Sleep(1000); 
  21.             t.Abort(); 
  22.             t.Join();//阻塞Main线程,直到t终止 
  23.             Console.ReadKey(); 
  24.         } 
  25.     } 
  26.     // 
  27.     class Guest 
  28.     { 
  29.         public string Name { getset; } 
  30.         public int Age { getset; } 
  31.  
  32.         public void DoSomething() 
  33.         { 
  34.             while (true
  35.             { 
  36.                 Console.WriteLine("{0}--{1}", Name, Age); 
  37.             } 
  38.         } 
  39.     } 
这个还是使用ThreadStart委托,对方法进行了一个封装。

两种方法,可随意选择,***种貌似简洁一点。

3.线程同步

线程同步的方法有很多很多种volatile、Lock、InterLock、Monitor、Mutex、ReadWriteLock...

这里用lock说明问题:在哪里同步,用什么同步,同步谁?

首先感受下不同步会出现的问题:

代码就是下面的代码去掉lock块。

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. using System.Threading; 
  6.  
  7. namespace 多线程3_同步2 
  8.     class Program 
  9.     { 
  10.         static object obj = new object();//同步用 
  11.  
  12.         static int balance = 500; 
  13.  
  14.         static void Main(string[] args) 
  15.         { 
  16.             Thread t1 = new Thread(new ThreadStart(Credit)); 
  17.             t1.Start(); 
  18.  
  19.             Thread t2 = new Thread(new ThreadStart(Debit)); 
  20.             t2.Start(); 
  21.  
  22.             Console.ReadKey(); 
  23.         } 
  24.  
  25.         static void Credit() 
  26.         { 
  27.             for (int i = 0; i < 15; i++) 
  28.             { 
  29.                 lock (obj) 
  30.                 { 
  31.                     balance += 100; 
  32.                     Console.WriteLine("After crediting,balance is {0}", balance); 
  33.                 } 
  34.             } 
  35.         } 
  36.         static void Debit() 
  37.         { 
  38.             for (int i = 0; i < 15; i++) 
  39.             { 
  40.                 lock (obj) 
  41.                 { 
  42.                     balance -= 100; 
  43.                     Console.WriteLine("After debiting,balance is {0}", balance); 
  44.                 } 
  45.             } 
  46.         } 
  47.     } 
小结:多线程调用函数就是这样。在Winform中,控件绑定到特定的线程,从另一个线程更新控件,不应该直接调用该控件的成员,这个非常有用。

原文链接:http://www.cnblogs.com/DebugLZQ/archive/2012/11/11/2765487.html

责任编辑:彭凡 来源: 博客园
相关推荐

2010-03-15 19:37:00

Java多线程同步

2009-10-27 12:20:06

VB.NET多线程应用

2009-02-01 09:06:15

.NET多线程.NET线程管理

2009-10-09 17:01:32

VB.NET多线程

2010-04-27 17:23:34

AIX系统

2011-12-15 11:03:21

JavaNIO

2011-06-30 17:31:32

Qt 多线程 信号

2010-03-03 09:12:25

.NET字符串拘留池

2009-07-21 17:09:47

ASP.NET多线程

2017-03-08 16:25:54

Linux多线程函数

2009-11-23 14:44:22

PHP 5.0构造函数

2009-12-14 14:32:50

Ruby线程局部域变量

2009-07-28 15:30:34

ASP.NET多线程

2009-07-28 11:34:02

ASP.NET架设

2009-10-22 11:25:08

CLR函数压缩

2009-04-24 09:14:20

.NET多线程锁机制

2010-04-14 09:20:26

.NET多线程

2010-01-18 18:06:07

VB.NET多线程

2009-10-23 09:26:09

VB.NET多线程

2009-10-12 16:08:14

VB.NET访问注册表
点赞
收藏

51CTO技术栈公众号