欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#简单多线程同步和优先权用法实例

程序员文章站 2023-12-20 13:34:46
本文实例讲述了c#简单多线程同步和优先权用法。分享给大家供大家参考。具体分析如下: 本文实例题目如下: 麦当劳有两个做汉堡的厨师(工号:11,12)和三个销售人员(工号...

本文实例讲述了c#简单多线程同步和优先权用法。分享给大家供大家参考。具体分析如下:

本文实例题目如下:

麦当劳有两个做汉堡的厨师(工号:11,12)和三个销售人员(工号:21,22,23)。
厨师生产汉堡,并负责将做好的汉堡放入货架,货架台大小有限,最多放6个汉堡,11和12不能同时往货架台上放汉堡,11具有优先权。
销售人员负责销售食品,三个销售人员取食品时,货架不能为空,三人不能同时取,23优先权最高,21最低。21卖的最快,取得频率最高,22次之。
一天的工作量是销售70个汉堡。

这里先来了解一些概念:
  
阻塞:函数返回结果之前,线程被挂起
非阻塞:函数执行完立即返回,不会阻塞线程
同步:函数没有执行完不返回,线程被挂起;
异步:函数立即返回,结果通过事件或是信号通知调用者;
 
同步消息处理就好比linux中简单的read/write操作,它们需要等待这操作成功才能返回;而异步处理机制就是类似于select/poll之类的多路复用io操作,当所关注的消息被触发时,由消息触发机制通知触发对消息的处理.

进程:当一个程序运行时,它就是一个进程,进程包括运行中的程序所使用到的内存和系统资源,同时一个进程可以包括多个线程

线程:线程是程序中的一个执行流,每个线程都有自己的专有寄存器(栈指针、程序计数器等),但代码区是共享的,即不同的线程可以执行同样的函数。

多线程:多线程是指程序中包含多个执行流,即在一个程序中可以同时运行多个不同的线程来执行不同的任务,也就是说允许单个程序创建多个并行执行的线程来完成各自的任务。时间片有cpu分配运行!

thread主要方法:strart(),sleep(int),abort(),suspend(),resume()

线程优先级:在c#应用程序中,用户可以设定5个不同的优先级,由高到低分别是highest,abovenormal,normal,belownormal,lowest,在创建线程时如果不指定优先级,那么系统默认为threadpriority.normal。

线程同步(framework中已经为我们提供了三个加锁的机制,分别是monitor类、lock关键字和mutex类。

① c#提供了一个关键字lock,它可以把一段代码定义为互斥段(critical section),互斥段在一个时刻内只允许一个线程进入执行,而其他线程必须等待。
在c#中,关键字lock定义如下:

lock(expression表达式) statement_block

② monitor主要用法

monitor.enter(obj);
(expression)
monitor.exit(obj);

③ mutex用法

mutex mutex = new mutex();
mutex.waitone();
(expression)
mutex.releasemutex();

举个lock互斥例子(包括多线程使用和多线程优先权、同步使用):

using system; 
using system.collections.generic; 
using system.text; 
using system.threading; 
namespace testthread 
{ 
  class program 
  { 
    private static object lockobject = new object(); 
    private static object lockobject2 = new object(); 
    private static int igethbnum = 0; 
    private static int iputhbnum = 0; 
    private static int count21 = 0; 
    private static int count22 = 0; 
    private static int count23 = 0; 
    private static int count11 = 0; 
    private static int count12 = 0; 
    static void main(string[] args) 
    { 
      console.writeline("主线程运行,线程id:" + thread.currentthread.managedthreadid.tostring()); 
      thread chushi1 = new thread(puthb); 
      chushi1.priority = threadpriority.abovenormal; 
      chushi1.name = "11"; 
      chushi1.start(); 
      thread chushi2 = new thread(puthb); 
      chushi2.priority = threadpriority.normal; 
      chushi2.name = "12"; 
      chushi2.start();     
 
      thread consume21 = new thread(gethb); 
      consume21.priority = threadpriority.normal; 
      consume21.name = "21"; 
      consume21.start(); 
      thread consume22 = new thread(gethb); 
      consume22.priority = threadpriority.normal; 
      consume22.name = "22"; 
      consume22.start(); 
      thread consume23 = new thread(gethb); 
      consume23.priority = threadpriority.normal; 
      consume23.name = "23"; 
      consume23.start(); 
      console.readkey(); 
    } 
    public static void puthb() 
    { 
      string strid = thread.currentthread.name.tostring(); 
      console.writeline("{0}厨师开始制作汉堡,,,", strid); 
      while (true) 
      { 
        if (iputhbnum >= 6) 
        { 
          console.writeline("厨师{0},最多放6个汉堡,请让销售员取再放!", strid); 
          thread.sleep(1000); 
        } 
        else 
        {    
          if (igethbnum >= 70 ||count11 + count12 >= 70) 
          { 
            if (strid == "11") 
            { 
              console.writeline("厨师{0},在货架放共放{1}汉堡!", strid, count11); 
            } 
            else if (strid == "12") 
            { 
              console.writeline("厨师{0},在货架放共放{1}汉堡!", strid, count12); 
            } 
            break; 
          } 
          lock (lockobject) 
          { 
            iputhbnum++; 
          } 
          if (strid == "11") 
          { 
            count11++; 
            console.writeline("厨师{0},在货架放已放{1}汉堡! 现在货架有{2}汉堡!", strid,count11, iputhbnum); 
          } 
          else if (strid == "12") 
          { 
            count12++; 
            console.writeline("厨师{0},在货架放已放{1}汉堡! 现在货架有{2}汉堡!", strid, count12, iputhbnum); 
          } 
        } 
      } 
    } 
    public static void gethb() 
    { 
      string strid = thread.currentthread.name.tostring(); 
      console.writeline("{0}销售员取汉堡,,,", strid); 
        while (true) 
        {  
          if (iputhbnum <= 0) 
          { 
            thread.sleep(1000); 
            console.writeline("{0}货架台已0个汉堡,请等待厨师制作!", strid);
          }
          else
          {
            lock (lockobject2)
            {
              igethbnum++;
              iputhbnum--;
            }
            if (strid == "23")
            {
              count23++;
              console.writeline("23号销售员已销售---{0}!",count23); 
              thread.sleep(3000); 
            }
            else if (strid == "22") 
            { 
              count22++; 
              console.writeline("22号销售员已销售---{0}!", count22); 
              thread.sleep(2000); 
            } 
            else if (strid == "21") 
            { 
              count21++; 
              console.writeline("21号销售员已销售---{0}!", count21); 
              thread.sleep(1000); 
            } 
          } 
          if (igethbnum >= 70) 
          { 
            console.writeline("销售完!"); 
            if (strid == "23") 
            { 
              console.writeline("23号销售员销售总数:{0}", count23); 
            } 
            else if (strid == "22") 
            { 
              console.writeline("22号销售员销售总数:{0}", count22); 
            } 
            else if (strid == "21") 
            { 
              console.writeline("21号销售员销售总数:{0}", count21); 
            } 
            break; 
          } 
        } 
    } 
  } 
} 

运行结果如下图所示:

C#简单多线程同步和优先权用法实例

lock可以用monitor,mutex替代:

monitor.enter(lockobject);
iputhbnum++;
monitor.exit(lockobject);

或者:

mutex1.waitone(); 
iputhbnum++;
mutex1.releasemutex();

希望本文所述对大家的c#程序设计有所帮助。

上一篇:

下一篇: