网站主页   操作系统    网络工程    服务器    网页制作    数据库    程序开发    网络安全    办公软件   
讲座日期: 本周六下午1点30分 抢座
讲座地点: 北大青鸟马甸校区
主讲老师: 王老师 金牌讲师
讲座主题: 网络安全
讲座内容: 检测、防御、黑客信息,如何过滤不安全的网站,如何防御黑客的进攻。
订座电话: 010-82011432/33
  您当前位置:主页 > 网络学院 > 程序开发 > C#教程 >

C#中使用Monitor类、Lock和Mutex类来同步多线程的执行




    在多线程中,为了使数据保持一致性必须要对数据或是访问数据的函数加锁,在数据库中这是很常见的,但是在程序中由于大部分都是单线程的程序,所以没有加锁的必要,但是在多线程中,为了保持数据的同步,一定要加锁,好在Framework中已经为我们提供了三个加锁的机制,分别是Monitor类、Lock关键字和Mutex类。
    其中Lock关键词用法比较简单,Monitor类和Lock的用法差不多。这两个都是锁定数据或是锁定被调用的函数。而Mutex则多用于锁定多线程间的同步调用。简单的说,Monitor和Lock多用于锁定被调用端,而Mutex则多用锁定调用端。
    程序中有两个线程thread1、thread2和一个TestFunc函数,TestFunc会打印出调用它的线程名和调用的时间(mm级的),两个线程分别以30mm和100mm来调用TestFunc这个函数。TestFunc执行的时间为50mm。程序如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace MonitorLockMutex
{
 class Program
 {
  #region variable
  Thread thread1 = null;
  Thread thread2 = null;
  Mutex mutex = null;
  #endregion
  static void Main(string[] args)
  {
   Program p = new Program();
   p.RunThread();
   Console.ReadLine();
  }
  public Program()
  {
   mutex = new Mutex();
   thread1 = new Thread(new ThreadStart(thread1Func));
   thread2 = new Thread(new ThreadStart(thread2Func));
  }
  public void RunThread()
  {
   thread1.Start();
   thread2.Start();
  }
  private void thread1Func()
  {
   for (int count = 0; count < 10; count++)
   {
    TestFunc("Thread1 have run " + count.ToString() + " times");
    Thread.Sleep(30);
   }
  }
  private void thread2Func()
  {
   for (int count = 0; count < 10; count++)
   {
    TestFunc("Thread2 have run " + count.ToString() + " times");
    Thread.Sleep(100);
   }
  }
  private void TestFunc(string str)
  {
   Console.WriteLine("{0} {1}", str, System.DateTime.Now.Millisecond.ToString());
   Thread.Sleep(50);
  }
 }
}
 
 
 
 
  private void TestFunc(string str)
  {
   lock (this)
   {
    Console.WriteLine("{0} {1}", str, System.DateTime.Now.Millisecond.ToString());
    Thread.Sleep(50);
   }
  }
 
 
  private void TestFunc(string str)
  {
   Monitor.Enter(this);
   Console.WriteLine("{0} {1}", str, System.DateTime.Now.Millisecond.ToString());
   Thread.Sleep(50);
   Monitor.Exit(this);
  }
 
 
 
    这就是锁定被调用函数的特性,即只能保证每次被一个线程调用,线程优先级高的调用的次数就多,低的就少,这就是所谓的强占式。
    下面让我们看看Mutex类的使用方法,以及与Monitor和Lock的区别。
 
  private void thread1Func()
  {
   for (int count = 0; count < 10; count++)
   {
    mutex.WaitOne();
    TestFunc("Thread1 have run " + count.ToString() + " times");
    mutex.ReleaseMutex();
   }
  }
 
  private void thread2Func()
  {
   for (int count = 0; count < 10; count++)
   {
    mutex.WaitOne();
    TestFunc("Thread2 have run " + count.ToString() + " times");
    mutex.ReleaseMutex();
   }
  }
 
  private void TestFunc(string str)
  {
   Console.WriteLine("{0} {1}", str, System.DateTime.Now.Millisecond.ToString());
   Thread.Sleep(50);
  }
 
运行结果如下:
 
 
  可以看出,Mutex只能互斥线程间的调用,但是不能互斥本线程的重复调用,即thread1中waitOne()只对thread2中的waitOne()起到互斥的作用,但是thread1并不受本wainOne()的影响,可以调用多次,只是在调用结束后调用相同次数的ReleaseMutex()就可以了。
 

private void thread1Func()
{
 for (int count = 0; count < 10; count++)
 {
  lock (this)
  {
   mutex.WaitOne();
   TestFunc("Thread1 have run " + count.ToString() + " times");
   mutex.ReleaseMutex();
  }
 }
}

private void thread2Func()
{
 for (int count = 0; count < 10; count++)
 {
  lock (this)
  {
   mutex.WaitOne();
   TestFunc("Thread2 have run " + count.ToString() + " times");
   mutex.ReleaseMutex();
  }
 }
}

 

 

上一篇:在C#程序设计中使用Win32类库  
下一篇:C#中使用委托
相关信息:

·关于用TextBox来验证数据的有效性(只允许输入带两位小 ·关于TextBox与ComboBox配合使用
·C#实现的18位身份证格式验证算法 ·在Vista中编程控制防火墙设定
·Visual C#2005中使用正则表达式 ·理解C# 3.0新特性之Extension方法浅议
·在Visual C#中定义和使用自己的特性 ·正确理解C#中的ref关键字
·Visual C#多线程参数传递浅析 ·《Effective C#》之用委托实现回调

Copyright © 2002-2015 版权所有
学校地址:北京市海淀区西三旗建材城中路29号北大青鸟
招生热线:010-82011433/32 京公网安备110102004704  京ICP备05043413号 京公网安备110102004704