From 04c93d86d9a60579a173c847ecd47a7c1babff02 Mon Sep 17 00:00:00 2001 From: MYP Date: Fri, 9 Oct 2020 12:39:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=85=E5=AE=B9=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DataStructureAndAlgorithm/10 队列.md | 572 --------- DataStructureAndAlgorithm/10 队列与多线程.md | 1093 ++++++++++++++++++ 2 files changed, 1093 insertions(+), 572 deletions(-) delete mode 100644 DataStructureAndAlgorithm/10 队列.md create mode 100644 DataStructureAndAlgorithm/10 队列与多线程.md diff --git a/DataStructureAndAlgorithm/10 队列.md b/DataStructureAndAlgorithm/10 队列.md deleted file mode 100644 index 0545858..0000000 --- a/DataStructureAndAlgorithm/10 队列.md +++ /dev/null @@ -1,572 +0,0 @@ - -# Task04£º¶ÓÁУ¨2Ì죩 - -¶ÓÁÐÒ²ÊÇÎÒÃǾ­³£Ê¹ÓõÄÒ»ÖÖÊý¾Ý½á¹¹£¬ÈçÏÂͼËùʾ£¬¹ºÎï½áÕË£¬È¥Ê³Ìôò·¹µÈ¶¼ÐèÒªÅŶӣ¬¶ø½áÕË»ò´ò·¹µÄ˳ÐòÓëÎÒÃÇÅŶӵÄ˳ÐòÊÇÏàͬµÄ£¬¼´Ë­ÏÈÅŶӾÍΪ˭ÏÈ·þÎñ¡£ - -![](https://img-blog.csdnimg.cn/20191223190948974.png) - -±ÈÈçÎÒÃÇ·¢ËÍÓʼþ¡¢´òÓ¡×ÊÁÏ£¬ÕâЩ¶¼ÊǶÓÁеľßÌåÓ¦Óá£ÎÒÃǰÑÐèÒª·¢Ë͵ÄÓʼþÏȷŵ½·¢ËͶÓÁÐÖУ¬È»ºó°´ÕÕ·ÅÈëµÄ˳Ðò½øÐз¢ËÍ£¬°ÑÐèÒª´òÓ¡µÄÎļþÏȷŵ½´òÓ¡¶ÓÁÐÖУ¬ È»ºó°´ÕÕ·ÅÈëµÄ˳Ðò½øÐдòÓ¡¡£ÏÂÃæÎÒÃǾÍÀ´Ïêϸ½éÉÜ¡°¶ÓÁС±ÕâÖÖÊý¾Ý½á¹¹¡£ - -## 1. ¶ÓÁе͍ÒåÓë²Ù×÷ - -**1.1 ¶ÓÁе͍Òå** - -²åÈ루Èë¶Ó£©ÔÚÒ»¶Ë£¨¶Ó⣩½øÐжøÉ¾³ý£¨³ö¶Ó£©ÔÚÁíÒ»¶Ë£¨¶ÓÊ×£©½øÐеÄÏßÐÔ±í¡£¼´ÏȽøÏȳö£¨First In First Out£©µÄÏßÐÔ±í¡£ - -Àý1 £ºÏßÐÔ±í`a0,a1,...,an`Èë¶ÓÓë³ö¶ÓÑÝʾ¡£ - -![˳Ðò±íÄ£ÄâÈë¶Ó¡¢³ö¶Ó](https://img-blog.csdnimg.cn/20191223191501199.png) - -![µ¥Á´±íÄ£ÄâÈë¶Ó¡¢³ö¶Ó](https://img-blog.csdnimg.cn/20191223191548730.png) - -ÈçÉÏËùʾ£¬¶ÓÁÐÒ²´æÔÚÁ½ÖÖʵÏÖ·½Ê½£¬ÕâÁ½ÖÖʵÏÖ·½Ê½µÄ¶Ô±ÈÒѾ­ÔÚÕ»ÓëµÝ¹é²¿·Ö½øÐÐÁ˽âÊÍ£¬ÔÚÕâÀï¾Í²»ÔÙ׸ÊöÁË¡£ - -**1.2 ¶ÓÁеIJÙ×÷** - -- Èë¶Ó²Ù×÷£º½«Êý¾ÝÔªËØ²åÈë¶Óβ¡£ -- ³ö¶Ó²Ù×÷£ºÒƳý¶ÓÊ×µÄÊý¾ÝÔªËØ¡£ -- ÊÇ·ñΪ¿Õ£ºÅж϶ÓÖÐÊÇ·ñ°üº¬Êý¾ÝÔªËØ¡£ -- µÃµ½¶Ó³¤£º»ñÈ¡¶ÓÖÐʵ¼Ê°üº¬Êý¾ÝÔªËØµÄ¸öÊý¡£ -- Çå¿Õ²Ù×÷£ºÒƳý¶ÓÖеÄËùÓÐÊý¾ÝÔªËØ¡£ -- »ñÈ¡¶ÓÊ×ÔªËØ¡£ - -![¶ÓÁнӿÚ](https://img-blog.csdnimg.cn/20191223191753857.png) - -```c -using System; - -namespace LinearStruct -{ - /// - /// ¶ÓÁеijéÏóÊý¾ÝÀàÐÍ - /// - /// ¶ÓÁÐÖÐÔªËØµÄÀàÐÍ - public interface IQueue where T : IComparable - { - /// - /// »ñÈ¡¶ÓÁÐÖÐʵ¼Ê°üº¬ÔªËصĸöÊý - /// - int Length { get; } - - /// - /// »ñÈ¡¶ÓÊ×ÔªËØ - /// - T QueueFront { get; } - - /// - /// Êý¾ÝÔªËØÈë¶Ó - /// - /// ÒªÈë¶ÓµÄÔªËØ - void EnQueue(T data); - - /// - /// Êý¾ÝÔªËØ³ö¶Ó - /// - void DeQueue(); - - /// - /// Åж϶ÓÁÐÖÐÊÇ·ñ°üº¬ÔªËØ - /// - /// Èç¹û°üº¬ÔªËØ·µ»Øfalse,·ñÔò·µ»Øtrue. - bool IsEmpty(); - - /// - /// ´Ó¶ÓÁÐÖÐÒÆ³ýËùÓÐÔªËØ - /// - void Clear(); - } -} -``` - - -## 2. ¶ÓÁеĴ洢ÓëʵÏÖ - -**2.1 ˳Ðò´æ´¢** - -˳Ðò¶ÓÁÐ - -˳Ðò¶ÓÁУº£¨Sequence Queue£©£ºÀûÓÃ˳Ðò±íʵÏֵĶÓÁС£ - -ʵÏÖ£º - -![˳Ðò¶ÓÁÐ](https://img-blog.csdnimg.cn/20191223192315765.png) - -```c -using System; - -namespace LinearStruct -{ - /// - /// ÓÃ˳Ðò´æ´¢½á¹¹ÊµÏֵĶÓÁÐ--˳Ðò¶ÓÁÐ - /// - /// ˳Ðò¶ÓÁÐÖÐÔªËØµÄÀàÐÍ - public class SeqQueue : IQueue where T : IComparable - { - private readonly SeqList _lst; - - /// - /// ³õʼ»¯SeqQueueÀàµÄÐÂʵÀý - /// - /// SeqQueueÖÐ×î¶à°üº¬ÔªËصĸöÊý - public SeqQueue(int max) - { - if (max <= 0) - throw new ArgumentOutOfRangeException(); - _lst = new SeqList(max); - } - - /// - /// »ñÈ¡SeqQueueÖÐ×î¶à°üº¬ÔªËصĸöÊý - /// - public int MaxSize - { - get { return _lst.MaxSize; } - } - - /// - /// »ñÈ¡SeqQueueÖÐʵ¼Ê°üº¬ÔªËصĸöÊý - /// - public int Length - { - get { return _lst.Length; } - } - - /// - /// »ñÈ¡SeqQueueÖеĶÓÊ×ÔªËØ - /// - public T QueueFront - { - get - { - if (_lst.IsEmpty()) - throw new Exception("¶ÓÁÐΪ¿Õ,²»Äܵõ½¶ÓÊ×ÔªËØ."); - return _lst[0]; - } - } - - /// - /// Êý¾ÝÔªËØÈë¶Ó - /// - /// ÒªÈë¶ÓµÄÔªËØ - public void EnQueue(T data) - { - if (_lst.Length == _lst.MaxSize) - throw new Exception("¶ÓÁÐÒÑÂú,²»ÄÜÈë¶Ó."); - _lst.Insert(_lst.Length, data); - } - - /// - /// Êý¾ÝÔªËØ³ö¶Ó - /// - public void DeQueue() - { - if (_lst.IsEmpty()) - throw new Exception("¶ÓÁÐΪ¿Õ,²»Äܳö¶Ó."); - _lst.Remove(0); - } - - /// - /// Åж϶ÓÁÐÖÐÊÇ·ñ°üº¬ÔªËØ - /// - /// Èç¹û°üº¬ÔªËØ·µ»Øfalse,·ñÔò·µ»Øtrue. - public bool IsEmpty() - { - return _lst.IsEmpty(); - } - - /// - /// ´Ó¶ÓÁÐÖÐÒÆ³ýËùÓÐÔªËØ - /// - public void Clear() - { - _lst.Clear(); - } - } -} -``` -Ñ­»·¶ÓÁÐ - -Ñ­»·¶ÓÁУ¨Circular Sequence Queue£©£ºÀûÓÃÊý×é²ÉÓÃÑ­»·µÄ·½Ê½ÊµÏֵĶÓÁС£ - -![Ñ­»·¶ÓÁйý³ÌÑÝʾ](https://img-blog.csdnimg.cn/20191223192517344.png) - - -ʵÏÖ£º - -![Ñ­»·¶ÓÁÐ](https://img-blog.csdnimg.cn/20191223192722855.png) - -```c -using System; - -namespace LinearStruct -{ - /// - /// ÓÃ˳Ðò´æ´¢½á¹¹ÊµÏֵĶÓÁÐ--Ñ­»·¶ÓÁÐ - /// - /// Ñ­»·¶ÓÁÐÖÐÔªËØµÄÀàÐÍ - public class CSeqQueue : IQueue where T : IComparable - { - private int _pFront; - private int _pRear; - private readonly T[] _dataset; - - /// - /// »ñÈ¡CSeqQueueÖÐʵ¼Ê°üº¬ÔªËصĸöÊý - /// - public int Length { get; private set; } - - /// - /// »ñÈ¡CSeqQueueÖÐ×î¶à°üº¬ÔªËصĸöÊý - /// - public int MaxSize { get; } - - /// - /// »ñÈ¡CSeqQueueÖеĶÓÊ×ÔªËØ - /// - public T QueueFront - { - get - { - if (Length == 0) - throw new Exception("¶ÓÁÐΪ¿Õ²»Äܵõ½¶ÓÊ×ÔªËØ."); - - return _dataset[_pFront]; - } - } - - /// - /// ³õʼ»¯CSeqQueueÀàµÄÐÂʵÀý - /// - /// CSeqQueueÖÐ×î¶à°üº¬ÔªËصĸöÊý - public CSeqQueue(int max) - { - if (max <= 0) - throw new ArgumentOutOfRangeException(); - MaxSize = max; - Length = 0; - _dataset = new T[MaxSize]; - _pFront = 0; - _pRear = 0; - } - - /// - /// Êý¾ÝÔªËØÈë¶Ó - /// - /// ÒªÈë¶ÓµÄÔªËØ - public void EnQueue(T data) - { - if (Length == MaxSize) - throw new Exception("¶ÓÁÐÒÑÂú,²»ÄÜÈë¶Ó."); - _dataset[_pRear] = data; - _pRear = (_pRear + 1)%MaxSize; - Length++; - } - - /// - /// Êý¾ÝÔªËØ³ö¶Ó - /// - public void DeQueue() - { - if (Length == 0) - throw new Exception("¶ÓÁÐΪ¿Õ,²»Äܳö¶Ó."); - _pFront = (_pFront + 1)%MaxSize; - Length--; - } - - /// - /// Åж϶ÓÁÐÖÐÊÇ·ñ°üº¬ÔªËØ - /// - /// Èç¹û°üº¬ÔªËØ·µ»Øfalse,·ñÔò·µ»Øtrue. - public bool IsEmpty() - { - return Length == 0; - } - - /// - /// ´Ó¶ÓÁÐÖÐÒÆ³ýËùÓÐÔªËØ - /// - public void Clear() - { - _pFront = 0; - _pRear = 0; - Length = 0; - } - } -} -``` - - -**2.2 Á´Ê½´æ´¢£¨Á´¶Ó£©** - -Á´¶Ó£ºÀûÓõ¥Á´±íʵÏֵĶÓÁС£ - -ʵÏÖ£º - - -![Á´¶Ó](https://img-blog.csdnimg.cn/20191223192940470.png) - -```c -using System; - -namespace LinearStruct -{ - /// - /// ÓÃÁ´Ê½´æ´¢½á¹¹ÊµÏֵĶÓÁÐ - /// - /// ¶ÓÁÐÖÐÔªËØµÄÀàÐÍ - public class LinkQueue : IQueue where T : IComparable - { - private readonly SLinkList _lst; - - /// - /// »ñÈ¡LinkQueueÖÐʵ¼Ê°üº¬ÔªËصĸöÊý - /// - public int Length - { - get { return _lst.Length; } - } - - /// - /// »ñÈ¡LinkQueueÖеĶÓÊ×ÔªËØ - /// - public T QueueFront - { - get - { - if (_lst.IsEmpty()) - throw new Exception("¶ÓÁÐΪ¿Õ,²»ÄÜÈ¡¶ÓÊ×ÔªËØ."); - return _lst[0]; - } - } - - /// - /// ³õʼ»¯LinkQueueÀàµÄÐÂʵÀý - /// - public LinkQueue() - { - _lst = new SLinkList(); - } - - /// - /// Êý¾ÝÔªËØÈë¶Ó - /// - /// ÒªÈë¶ÓµÄÔªËØ - public void EnQueue(T data) - { - _lst.InsertAtRear(data); - } - - /// - /// Êý¾ÝÔªËØ³ö¶Ó - /// - public void DeQueue() - { - if (_lst.IsEmpty()) - throw new Exception("¶ÓÁÐΪ¿Õ,²»Äܳö¶Ó."); - - _lst.Remove(0); - } - - /// - /// Åж϶ÓÁÐÖÐÊÇ·ñ°üº¬ÔªËØ - /// - /// Èç¹û°üº¬ÔªËØ·µ»Øfalse,·ñÔò·µ»Øtrue. - public bool IsEmpty() - { - return _lst.IsEmpty(); - } - - /// - /// ´Ó¶ÓÁÐÖÐÒÆ³ýËùÓÐÔªËØ - /// - public void Clear() - { - _lst.Clear(); - } - } -} -``` - - -## 3. Á·Ï°²Î¿¼´ð°¸ - -**1. Ä£ÄâÒøÐзþÎñµÄ³ÌÐò´úÂë** - -ÒÔÏ´úÂëΪ`C#`°æ±¾£º - -±íÊ¾ÒøÐжÓÁÐµÄ½Ó¿Ú -```c -using LinearStruct; - -namespace BankQueue -{ - public interface IBankQueue : IQueue - { - /// - /// »ñµÃ·þÎñºÅÂë - /// - /// ·þÎñºÅÂë - int GetCallnumber(); - int MaxSize { get; } - } -} -``` - -ÀûÓÃÁ´Ê½´æ´¢½á¹¹´æ´¢ÒøÐжÓÁÐ -```c -using LinearStruct; - -namespace BankQueue -{ - public class LinkBankQueue : LinkQueue, IBankQueue - { - public int Callnumber { get; private set; } - public int MaxSize { get; } - public int GetCallnumber() - { - if (IsEmpty() && Callnumber == 0) - { - Callnumber = 1; - } - else - { - Callnumber++; - } - return Callnumber; - } - public LinkBankQueue() - { - MaxSize = default(int); - Callnumber = 0; - } - } -} -``` - -ÀûÓÃ˳Ðò´æ´¢½á¹¹´æ´¢ÒøÐжÓÁÐ -```c -using LinearStruct; - -namespace BankQueue -{ - public class CSeqBankQueue : CSeqQueue, IBankQueue - { - public int Callnumber { get; private set; } - - public CSeqBankQueue(int size) : base(size) - { - Callnumber = 0; - } - public int GetCallnumber() - { - if (IsEmpty() && Callnumber == 0) - { - Callnumber = 1; - } - else - { - Callnumber++; - } - return Callnumber; - } - } -} -``` - -·þÎñ´°¿Ú -```c -using System; -using System.Threading; - -namespace BankQueue -{ - public class ServiceWindow - { - //·þÎñ¶ÓÁÐÊôÐÔ - public IBankQueue BankQ { get; set; } - - //Ï̷߳½·¨ - public void Service() - { - while (true) - { - lock (BankQ) - { - Thread.Sleep(2000); - if (!BankQ.IsEmpty()) - { - Console.WriteLine(); - Console.WriteLine("Çë{0}ºÅµ½{1}ºÅ´°¿Ú!", BankQ.QueueFront, Thread.CurrentThread.Name); - BankQ.DeQueue(); - } - } - } - } - } -} -``` - -¿Í»§¶Ë -```c -using System; -using System.Threading; - -namespace BankQueue -{ - class BankQueueApp - { - public static void Main(string[] args) - { - - try - { - IBankQueue bankQueue = null; - Console.WriteLine("ÇëÑ¡Ôñ´æ´¢½á¹¹µÄÀàÐÍ£º1.˳Ðò¶ÓÁÐ 2.Á´¶ÓÁÐ"); - string seleflag = Console.ReadLine(); - switch (seleflag) - { - case "1": - Console.Write("ÇëÊäÈë¶ÓÁпÉÈÝÄÉÈËÊý£º"); - int count = Convert.ToInt32(Console.ReadLine()); - bankQueue = new CSeqBankQueue(count); - break; - case "2": - bankQueue = new LinkBankQueue(); - break; - } - - int windowcount = 3; - ServiceWindow[] serviceWindows = new ServiceWindow[windowcount]; - Thread[] serviceThread = new Thread[windowcount]; - for (int i = 0; i < windowcount; i++) - { - serviceWindows[i] = new ServiceWindow(); - serviceWindows[i].BankQ = bankQueue; - serviceThread[i] = new Thread(serviceWindows[i].Service); - serviceThread[i].Name = (i + 1).ToString(); - serviceThread[i].Start(); - } - while (true) - { - Console.Write("Çëµã»÷´¥ÃþÆÁ»ñÈ¡ºÅÂ룺"); - Console.ReadLine(); - if (bankQueue != null && (bankQueue.Length < bankQueue.MaxSize || seleflag == "2")) - { - int callnumber = bankQueue.GetCallnumber(); - Console.WriteLine("ÄúµÄºÅÂëÊÇ£º{0}£¬ÄãÇ°ÃæÓÐ{1}룬ÇëµÈ´ý£¡", - callnumber, bankQueue.Length); - bankQueue.EnQueue(callnumber); - } - else - Console.WriteLine("ÏÖÔÚÒµÎñ·±Ã¦£¬ÇëÉÔºóÔÙÀ´£¡"); - Console.WriteLine(); - } - } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - } - } - } -} -``` \ No newline at end of file diff --git a/DataStructureAndAlgorithm/10 队列与多线程.md b/DataStructureAndAlgorithm/10 队列与多线程.md new file mode 100644 index 0000000..f3903c8 --- /dev/null +++ b/DataStructureAndAlgorithm/10 队列与多线程.md @@ -0,0 +1,1093 @@ + + +# 10 ¶ÓÁÐÓë¶àÏß³Ì + +**֪ʶ½á¹¹£º** + + +![ͼ1 ֪ʶ½á¹¹](https://img-blog.csdnimg.cn/20201009121330374.png) + +¶ÓÁÐÊÇÎÒÃǾ­³£Ê¹ÓõÄÒ»ÖÖÊý¾Ý½á¹¹£¬ÈçÏÂͼËùʾ£¬¹ºÎï½áÕË£¬È¥Ê³Ìôò·¹µÈ¶¼ÐèÒªÅŶӣ¬¶ø½áÕË»ò´ò·¹µÄ˳ÐòÓëÎÒÃÇÅŶӵÄ˳ÐòÊÇÏàͬµÄ£¬¼´Ë­ÏÈÅŶӾÍΪ˭ÏÈ·þÎñ¡£ + +![ͼ2 ¶ÓÁоÙÀý](https://img-blog.csdnimg.cn/20191223190948974.png) + +±ÈÈçÎÒÃÇ·¢ËÍÓʼþ¡¢´òÓ¡×ÊÁÏ£¬ÕâЩ¶¼ÊǶÓÁеľßÌåÓ¦Óá£ÎÒÃǰÑÐèÒª·¢Ë͵ÄÓʼþÏȷŵ½·¢ËͶÓÁÐÖУ¬È»ºó°´ÕÕ·ÅÈëµÄ˳Ðò½øÐз¢ËÍ£¬°ÑÐèÒª´òÓ¡µÄÎļþÏȷŵ½´òÓ¡¶ÓÁÐÖУ¬ È»ºó°´ÕÕ·ÅÈëµÄ˳Ðò½øÐдòÓ¡¡£ÏÂÃæÎÒÃǾÍÀ´Ïêϸ½éÉÜ¡°¶ÓÁС±ÕâÖÖÊý¾Ý½á¹¹¡£ + + +--- +## 1. ¶ÓÁе͍ÒåÓë²Ù×÷ + +### 1.1 ¶ÓÁе͍Òå + +²åÈ루Èë¶Ó£©ÔÚÒ»¶Ë£¨¶Ó⣩½øÐжøÉ¾³ý£¨³ö¶Ó£©ÔÚÁíÒ»¶Ë£¨¶ÓÊ×£©½øÐеÄÏßÐÔ±í¡£¼´ÏȽøÏȳö£¨First In First Out£©µÄÏßÐÔ±í¡£ + +¡¾Àý×Ó¡¿ÏßÐÔ±í`$(a_0,a_1,\dots,a_{n-1})$`Èë¶ÓÓë³ö¶ÓÑÝʾ¡£ + +![ͼ3 ˳Ðò±íÄ£ÄâÈë¶Ó¡¢³ö¶Ó](https://img-blog.csdnimg.cn/20191223191501199.png) + +![ͼ4 µ¥Á´±íÄ£ÄâÈë¶Ó¡¢³ö¶Ó](https://img-blog.csdnimg.cn/20191223191548730.png) + +### 1.2 ¶ÓÁеIJÙ×÷ + +- Èë¶Ó²Ù×÷£º½«Êý¾ÝÔªËØ²åÈë¶Óβ¡£ +- ³ö¶Ó²Ù×÷£ºÒƳý¶ÓÊ×µÄÊý¾ÝÔªËØ¡£ +- ÊÇ·ñΪ¿Õ£ºÅж϶ÓÖÐÊÇ·ñ°üº¬Êý¾ÝÔªËØ¡£ +- µÃµ½¶Ó³¤£º»ñÈ¡¶ÓÖÐʵ¼Ê°üº¬Êý¾ÝÔªËØµÄ¸öÊý¡£ +- Çå¿Õ²Ù×÷£ºÒƳý¶ÓÖеÄËùÓÐÊý¾ÝÔªËØ¡£ +- »ñÈ¡¶ÓÊ×ÔªËØ¡£ + +![ͼ5 ¶ÓÁнӿÚ](https://img-blog.csdnimg.cn/20191223191753857.png) + +```c +using System; +namespace LinearStruct +{ + /// + /// ¶ÓÁеijéÏóÊý¾ÝÀàÐÍ + /// + /// ¶ÓÁÐÖÐÔªËØµÄÀàÐÍ + public interface IQueue where T : IComparable + { + /// + /// »ñÈ¡¶ÓÁÐÖÐʵ¼Ê°üº¬ÔªËصĸöÊý + /// + int Length { get; } + /// + /// »ñÈ¡¶ÓÊ×ÔªËØ + /// + T QueueFront { get; } + /// + /// Êý¾ÝÔªËØÈë¶Ó + /// + /// ÒªÈë¶ÓµÄÔªËØ + void EnQueue(T data); + /// + /// Êý¾ÝÔªËØ³ö¶Ó + /// + void DeQueue(); + /// + /// Åж϶ÓÁÐÖÐÊÇ·ñ°üº¬ÔªËØ + /// + /// Èç¹û°üº¬ÔªËØ·µ»Øfalse,·ñÔò·µ»Øtrue. + bool IsEmpty(); + /// + /// ´Ó¶ÓÁÐÖÐÒÆ³ýËùÓÐÔªËØ + /// + void Clear(); + } +} +``` + +--- +## 2. ¶ÓÁеÄ˳Ðò´æ´¢ + +### 2.1 ˳Ðò¶ÓÁÐ + +˳Ðò¶ÓÁУ¨Sequence Queue£©£ºÀûÓÃ˳Ðò±íʵÏֵĶÓÁС£ + +ʵÏÖ£º + +![ͼ6 ˳Ðò¶ÓÁÐ](https://img-blog.csdnimg.cn/20201008201245788.png) + +```c +using System; +namespace LinearStruct +{ + /// + /// ÓÃ˳Ðò´æ´¢½á¹¹ÊµÏֵĶÓÁÐ--˳Ðò¶ÓÁÐ + /// + /// ˳Ðò¶ÓÁÐÖÐÔªËØµÄÀàÐÍ + public class SeqQueue : IQueue where T : IComparable + { + private readonly SeqList _lst; + + /// + /// ³õʼ»¯SeqQueueÀàµÄÐÂʵÀý + /// + /// SeqQueueÖÐ×î¶à°üº¬ÔªËصĸöÊý + public SeqQueue(int max) + { + if (max <= 0) + throw new ArgumentOutOfRangeException(); + _lst = new SeqList(max); + } + /// + /// »ñÈ¡SeqQueueÖÐ×î¶à°üº¬ÔªËصĸöÊý + /// + public int MaxSize + { + get { return _lst.MaxSize; } + } + /// + /// »ñÈ¡SeqQueueÖÐʵ¼Ê°üº¬ÔªËصĸöÊý + /// + public int Length + { + get { return _lst.Length; } + } + /// + /// »ñÈ¡SeqQueueÖеĶÓÊ×ÔªËØ + /// + public T QueueFront + { + get + { + if (_lst.IsEmpty()) + throw new Exception("¶ÓÁÐΪ¿Õ,²»Äܵõ½¶ÓÊ×ÔªËØ."); + return _lst[0]; + } + } + /// + /// Êý¾ÝÔªËØÈë¶Ó + /// + /// ÒªÈë¶ÓµÄÔªËØ + public void EnQueue(T data) + { + if (_lst.Length == _lst.MaxSize) + throw new Exception("¶ÓÁÐÒÑÂú,²»ÄÜÈë¶Ó."); + _lst.Insert(_lst.Length, data); + } + /// + /// Êý¾ÝÔªËØ³ö¶Ó + /// + public void DeQueue() + { + if (_lst.IsEmpty()) + throw new Exception("¶ÓÁÐΪ¿Õ,²»Äܳö¶Ó."); + _lst.Remove(0); + } + /// + /// Åж϶ÓÁÐÖÐÊÇ·ñ°üº¬ÔªËØ + /// + /// Èç¹û°üº¬ÔªËØ·µ»Øfalse,·ñÔò·µ»Øtrue. + public bool IsEmpty() + { + return _lst.IsEmpty(); + } + /// + /// ´Ó¶ÓÁÐÖÐÒÆ³ýËùÓÐÔªËØ + /// + public void Clear() + { + _lst.Clear(); + } + } +} +``` + +Ó¦Ó㺠+ +```c +class Program +{ + static void Main(string[] args) + { + QueueTest(new SeqQueue(20)); + } + private static void QueueTest(IQueue queue) + { + queue.EnQueue("a1"); + queue.EnQueue("a2"); + queue.EnQueue("a3"); + while (queue.IsEmpty() == false) + { + Console.WriteLine(queue.QueueFront); + queue.DeQueue(); + } + // a1 + // a2 + // a3 + } +} +``` + +### 2.2 Ñ­»·¶ÓÁÐ + +Ñ­»·¶ÓÁУ¨Circular Sequence Queue£©£ºÀûÓÃÊý×é²ÉÓÃÑ­»·µÄ·½Ê½ÊµÏֵĶÓÁС£ + +¡¾Àý×Ó¡¿ÏßÐÔ±í`$(a_0,a_1,\dots,a_{n-1})$`Èë¶ÓÓë³ö¶ÓÑÝʾ¡£ + +![ͼ7 Ñ­»·¶ÓÁйý³ÌÑÝʾ](https://img-blog.csdnimg.cn/20201008202717909.png) + +ʵÏÖ£º + +![ͼ8 Ñ­»·¶ÓÁÐ](https://img-blog.csdnimg.cn/20201008202616468.png) + +```c +using System; +namespace LinearStruct +{ + /// + /// ÓÃ˳Ðò´æ´¢½á¹¹ÊµÏֵĶÓÁÐ--Ñ­»·¶ÓÁÐ + /// + /// Ñ­»·¶ÓÁÐÖÐÔªËØµÄÀàÐÍ + public class CSeqQueue : IQueue where T : IComparable + { + private int _pFront; + private int _pRear; + private readonly T[] _dataset; + + /// + /// »ñÈ¡CSeqQueueÖÐʵ¼Ê°üº¬ÔªËصĸöÊý + /// + public int Length { get; private set; } + /// + /// »ñÈ¡CSeqQueueÖÐ×î¶à°üº¬ÔªËصĸöÊý + /// + public int MaxSize { get; } + /// + /// »ñÈ¡CSeqQueueÖеĶÓÊ×ÔªËØ + /// + public T QueueFront + { + get + { + if (Length == 0) + throw new Exception("¶ÓÁÐΪ¿Õ²»Äܵõ½¶ÓÊ×ÔªËØ."); + + return _dataset[_pFront]; + } + } + /// + /// ³õʼ»¯CSeqQueueÀàµÄÐÂʵÀý + /// + /// CSeqQueueÖÐ×î¶à°üº¬ÔªËصĸöÊý + public CSeqQueue(int max) + { + if (max <= 0) + throw new ArgumentOutOfRangeException(); + MaxSize = max; + Length = 0; + _dataset = new T[MaxSize]; + _pFront = 0; + _pRear = 0; + } + /// + /// Êý¾ÝÔªËØÈë¶Ó + /// + /// ÒªÈë¶ÓµÄÔªËØ + public void EnQueue(T data) + { + if (Length == MaxSize) + throw new Exception("¶ÓÁÐÒÑÂú,²»ÄÜÈë¶Ó."); + _dataset[_pRear] = data; + _pRear = (_pRear + 1)%MaxSize; + Length++; + } + /// + /// Êý¾ÝÔªËØ³ö¶Ó + /// + public void DeQueue() + { + if (Length == 0) + throw new Exception("¶ÓÁÐΪ¿Õ,²»Äܳö¶Ó."); + _pFront = (_pFront + 1)%MaxSize; + Length--; + } + /// + /// Åж϶ÓÁÐÖÐÊÇ·ñ°üº¬ÔªËØ + /// + /// Èç¹û°üº¬ÔªËØ·µ»Øfalse,·ñÔò·µ»Øtrue. + public bool IsEmpty() + { + return Length == 0; + } + /// + /// ´Ó¶ÓÁÐÖÐÒÆ³ýËùÓÐÔªËØ + /// + public void Clear() + { + _pFront = 0; + _pRear = 0; + Length = 0; + } + } +} +``` + +Ó¦Ó㺠+ +```c +class Program +{ + static void Main(string[] args) + { + QueueTest(new CSeqQueue(20)); + } + private static void QueueTest(IQueue queue) + { + queue.EnQueue("a1"); + queue.EnQueue("a2"); + queue.EnQueue("a3"); + while (queue.IsEmpty() == false) + { + Console.WriteLine(queue.QueueFront); + queue.DeQueue(); + } + // a1 + // a2 + // a3 + } +} +``` + +--- +## 3. ¶ÓÁеÄÁ´Ê½´æ´¢ + +Á´¶Ó£ºÀûÓõ¥Á´±íʵÏֵĶÓÁС£ + +ÀûÓõ¥Á´±íʵÏÖ£º + +![ͼ9 Á´¶Ó](https://img-blog.csdnimg.cn/20201008202900384.png) + +```c +using System; +namespace LinearStruct +{ + /// + /// ÓÃÁ´Ê½´æ´¢½á¹¹ÊµÏֵĶÓÁÐ + /// + /// ¶ÓÁÐÖÐÔªËØµÄÀàÐÍ + public class LinkQueue : IQueue where T : IComparable + { + private readonly SLinkList _lst; + /// + /// »ñÈ¡LinkQueueÖÐʵ¼Ê°üº¬ÔªËصĸöÊý + /// + public int Length + { + get { return _lst.Length; } + } + /// + /// »ñÈ¡LinkQueueÖеĶÓÊ×ÔªËØ + /// + public T QueueFront + { + get + { + if (_lst.IsEmpty()) + throw new Exception("¶ÓÁÐΪ¿Õ,²»ÄÜÈ¡¶ÓÊ×ÔªËØ."); + return _lst[0]; + } + } + /// + /// ³õʼ»¯LinkQueueÀàµÄÐÂʵÀý + /// + public LinkQueue() + { + _lst = new SLinkList(); + } + /// + /// Êý¾ÝÔªËØÈë¶Ó + /// + /// ÒªÈë¶ÓµÄÔªËØ + public void EnQueue(T data) + { + _lst.InsertAtRear(data); + } + /// + /// Êý¾ÝÔªËØ³ö¶Ó + /// + public void DeQueue() + { + if (_lst.IsEmpty()) + throw new Exception("¶ÓÁÐΪ¿Õ,²»Äܳö¶Ó."); + + _lst.Remove(0); + } + /// + /// Åж϶ÓÁÐÖÐÊÇ·ñ°üº¬ÔªËØ + /// + /// Èç¹û°üº¬ÔªËØ·µ»Øfalse,·ñÔò·µ»Øtrue. + public bool IsEmpty() + { + return _lst.IsEmpty(); + } + /// + /// ´Ó¶ÓÁÐÖÐÒÆ³ýËùÓÐÔªËØ + /// + public void Clear() + { + _lst.Clear(); + } + } +} +``` + +Óõ¥Á´±íʵÏÖ¶ÓÁУ¬Èë¶Óʱ»á½øÐÐÁ´±íµÄ±éÀú²Ù×÷£¬·Ç³£ºÄʱ¡£¹Êʵ¼ÊÓ¦ÓÃÖУ¬Á´¶Ó³£³£ÓÃÑ­»·Á´±íÀ´ÊµÏÖ¡£ + +![ͼ10 Á´¶Ó](https://img-blog.csdnimg.cn/2020100820325784.png) + +```c +using System; +namespace LinearStruct +{ + /// + /// ÓÃÁ´Ê½´æ´¢½á¹¹ÊµÏֵĶÓÁÐ + /// + /// ¶ÓÁÐÖÐÔªËØµÄÀàÐÍ + public class LinkQueue : IQueue where T : IComparable + { + private readonly CLinkList _lst; + /// + /// »ñÈ¡LinkQueueÖÐʵ¼Ê°üº¬ÔªËصĸöÊý + /// + public int Length + { + get { return _lst.Length; } + } + /// + /// »ñÈ¡LinkQueueÖеĶÓÊ×ÔªËØ + /// + public T QueueFront + { + get + { + if (_lst.IsEmpty()) + throw new Exception("¶ÓÁÐΪ¿Õ,²»ÄÜÈ¡¶ÓÊ×ÔªËØ."); + return _lst[0]; + } + } + /// + /// ³õʼ»¯LinkQueueÀàµÄÐÂʵÀý + /// + public LinkQueue() + { + _lst = new CLinkList(); + } + /// + /// Êý¾ÝÔªËØÈë¶Ó + /// + /// ÒªÈë¶ÓµÄÔªËØ + public void EnQueue(T data) + { + _lst.InsertAtRear(data); + } + /// + /// Êý¾ÝÔªËØ³ö¶Ó + /// + public void DeQueue() + { + if (_lst.IsEmpty()) + throw new Exception("¶ÓÁÐΪ¿Õ,²»Äܳö¶Ó."); + + _lst.Remove(0); + } + /// + /// Åж϶ÓÁÐÖÐÊÇ·ñ°üº¬ÔªËØ + /// + /// Èç¹û°üº¬ÔªËØ·µ»Øfalse,·ñÔò·µ»Øtrue. + public bool IsEmpty() + { + return _lst.IsEmpty(); + } + /// + /// ´Ó¶ÓÁÐÖÐÒÆ³ýËùÓÐÔªËØ + /// + public void Clear() + { + _lst.Clear(); + } + } +} +``` + + +Ó¦Ó㺠+ +```c +class Program +{ + static void Main(string[] args) + { + QueueTest(new LinkQueue()); + } + private static void QueueTest(IQueue queue) + { + queue.EnQueue("a1"); + queue.EnQueue("a2"); + queue.EnQueue("a3"); + while (queue.IsEmpty() == false) + { + Console.WriteLine(queue.QueueFront); + queue.DeQueue(); + } + // a1 + // a2 + // a3 + } +} +``` + +--- +## 4. ½ø³Ì + +### 4.1 ½ø³ÌµÄ¶¨Òå + +½ø³ÌÊdzÌÐòµÄ¶¯Ì¬Ö´Ðйý³Ì£¬ÊDzÙ×÷ϵͳ½øÐÐ×ÊÔ´·ÖÅäºÍµ÷¶ÈµÄ»ù±¾µ¥Î»¡£ + +### 4.2 C# ¶Ô½ø³ÌµÄ¹ÜÀí + +ÎÒÃÇͨ¹ýÒ»¸ö¾ßÌåµÄʵÀý½øÐÐ˵Ã÷¡£ + +![ͼ11 ½ø³ÌʵÀý](https://img-blog.csdnimg.cn/20201009093558675.png) + +**£¨1£©ËùÔÚÃüÃû¿Õ¼ä** + +```c +using System.Diagnostics; +``` + +**£¨2£©½ø³ÌµÄ´´½¨** + +```c +//³õʼ»¯ProcessÀàµÄÐÂʵÀý¡£ +Process pros = new Process(); +``` + +**£¨3£©½ø³ÌµÄÆô¶¯** + +```c +private void buttonStartNotepad_Click(object sender, EventArgs e) +{ + Process pros = new Process(); + //»ñÈ¡»òÉèÖÃÒªÆô¶¯µÄÓ¦ÓóÌÐò»òÎĵµ¡£ + pros.StartInfo.FileName = "Notepad.exe"; + //»ñÈ¡»òÉèÖÃÆô¶¯½ø³ÌʱʹÓõĴ°¿Ú״̬¡£ + pros.StartInfo.WindowStyle = ProcessWindowStyle.Minimized; + //Æô¶¯´Ë Process ×é¼þµÄ StartInfo ÊôÐÔÖ¸¶¨µÄ½ø³Ì×ÊÔ´£¬²¢½«ÆäÓë¸Ã×é¼þ¹ØÁª¡£ + pros.Start(); +} +``` + +**£¨4£©½ø³ÌµÄ²éѯ** +```c +private void InitDataGridView() +{ + dataGridView.ColumnCount = 4; + dataGridView.Columns[0].HeaderText = "½ø³ÌºÅ"; + dataGridView.Columns[1].HeaderText = "½ø³ÌÃû³Æ"; + dataGridView.Columns[2].HeaderText = "ËùÕ¼ÄÚ´æ"; + dataGridView.Columns[3].HeaderText = "Æô¶¯Ê±¼ä"; +} + +private void buttonViewProcess_Click(object sender, EventArgs e) +{ + dataGridView.RowCount = 0; + //Ϊ±¾µØ¼ÆËã»úÉϵÄÿ¸ö½ø³Ì×ÊÔ´´´½¨Ò»¸öÐ嵀 Process ×é¼þ¡£ + Process[] proses = Process.GetProcesses(); + foreach (Process p in proses) + { + int id = p.Id;//½ø³ÌºÅ + string pName = p.ProcessName;//½ø³ÌÃû³Æ + long memory = p.WorkingSet64;//ËùÕ¼ÎïÀíÄÚ´æ + try + { + DateTime startTime = p.StartTime;//½ø³ÌÆô¶¯µÄʱ¼ä + dataGridView.Rows.Add(); + int loc = dataGridView.RowCount - 1; + dataGridView.Rows[loc].Cells[0].Value = id; + dataGridView.Rows[loc].Cells[1].Value = pName; + dataGridView.Rows[loc].Cells[2].Value = memory; + dataGridView.Rows[loc].Cells[3].Value = startTime; + } + catch + { + ; + } + } +} +``` + +**£¨5£©½ø³ÌµÄÖÕÖ¹** + +```c +private void buttonAbortNotepad_Click(object sender, EventArgs e) +{ + Process[] pes = Process.GetProcessesByName("Notepad"); + foreach (Process p in pes) + { + //ָʾ Process ×é¼þÔÚÖ¸¶¨µÄºÁÃëÊýÄڵȴý¹ØÁª½ø³ÌÍ˳ö¡£ + p.WaitForExit(1000); + //Á¢¼´Í£Ö¹¹ØÁªµÄ½ø³Ì¡£ + p.Kill(); + } +} +``` + +--- +## 5. Ïß³Ì + +### 5.1 Ï̵͍߳Òå + +ͬһ¸ö½ø³ÌÓÖ¿É»®·ÖΪÈô¸É¸ö¶ÀÁ¢µÄÖ´ÐÐÁ÷³ÆÖ®ÎªỊ̈߳¬Ïß³ÌÊÇCPU½øÐÐ×ÊÔ´·ÖÅäºÍµ÷¶ÈµÄ»ù±¾µ¥Î»¡£ + +### 5.2 C#¶ÔÏ̵߳ĹÜÀí + +**£¨1£©ËùÔÚÃüÃû¿Õ¼ä** + +```c +using System.Threading; +``` + +**£¨2£©Ï̵߳Ĵ´½¨** + +```c +//³õʼ»¯ Thread ÀàµÄÐÂʵÀý¡£ +Thread thread1 = new Thread(enterPoint1); +//enterPoint1,enterPoint2 ÊÇÏß³ÌÒªÖ´Ðеķ½·¨¡£ +Thread thread2 = new Thread(enterPoint2); +``` + +**£¨3£©Ïß³ÌµÄÆô¶¯** + +```c +//µ¼Ö²Ù×÷ϵͳ½«µ±Ç°ÊµÀýµÄ״̬¸ü¸ÄΪ ThreadState.Running¡£ +thread1.Start(); +thread2.Start(); +``` + +**£¨4£©Ï̵߳ÄÐÝÃß** + +```c +//½«µ±Ç°Ïß³Ì¹ÒÆðÖ¸¶¨µÄʱ¼ä¡£¸ÃÓï¾äÏß³ÌÐÝÃß1ÃëÖÓ¡£ +Thread.Sleep(1000); +``` + +**£¨5£©Ï̵߳ĺϲ¢** + +```c +//×èÖ¹µ÷ÓÃỊ̈߳¬Ö±µ½Ä³¸öÏß³ÌÖÕֹΪֹ¡£ +thread1.Join(); +//×èÖ¹µ÷ÓÃỊ̈߳¬Ö±µ½Ä³¸öÏß³ÌÖÕÖ¹»ò¾­¹ýÁËÖ¸¶¨Ê±¼äΪֹ¡£ +//Èô thread2 ÔËÐйý³ÌÖÐÐèÒªµÈ´ý thread1 ½áÊøºó²ÅÄܼÌÐøÖ´ÐУ¬ +//¿ÉÔÚ thread2 µÄÄ£¿éÖе÷Óà thread1.Join()£¬Õâʱ thread2´¦ÓÚ×èÈû״̬¡£ +thread2.Join(1000); +``` + +**£¨6£©Ï̵߳ÄÖÕÖ¹** + +```c +//µ÷Óô˷½·¨Í¨³£»áÖÕÖ¹Ï̡߳£ +thread1.Abort(); +``` + +¡¾Àý×Ó¡¿Ã»Óй²Ïí×ÊÔ´µÄÇé¿öÏÂÁ½¸öÏ̵߳IJ¢ÐС£ + +```c +static class Test +{ + public static void Addition() + { + int sum = 0; + for (int i = 0; i <= 100; i++) + { + sum += i; + Console.WriteLine("sum:{0}", sum); + } + } + + public static void Division() + { + int div = 5050; + for (int i = 0; i <= 100; i++) + { + div -= i; + Console.WriteLine("div:{0}", div); + } + } +} +``` + +¿Í»§¶Ë´úÂ룺 +```c +class Program +{ + static void Main(string[] args) + { + Thread thread1 = new Thread(Test.Add); + Thread thread2 = new Thread(Test.SubStract); + thread1.Start(); + thread2.Start(); + Console.WriteLine("Main Thread Finish!"); + } +} +``` + +![ͼ12 ÔËÐнá¹û](https://img-blog.csdnimg.cn/20201009101630770.png) + + +¿Í»§¶Ë´úÂ룺 +```c +class Program +{ + static void Main(string[] args) + { + Thread thread1 = new Thread(Test.Add); + Thread thread2 = new Thread(Test.SubStract); + thread1.Start(); + thread2.Start(); + thread1.Join(); + thread2.Join(); + Console.WriteLine("Main Thread Finish!"); + } +} +``` + +![ͼ13 ÔËÐнá¹û](https://img-blog.csdnimg.cn/20201009101906707.png) + +**£¨7£©Ï̵߳Äͬ²½** + +- Ïß³Ìͬ²½µÄ¶¨Òå + +![ͼ14 Ïß³Ìͬ²½](https://img-blog.csdnimg.cn/20201009102338214.png) + + +`Thread1`ûÓÐÖ´ÐÐÍêÁÙ½çÇø´úÂ룬·ÖÅäµÄʱ¼äƬ½áÊø£¬Õâʱ`Thread2`Ö´ÐÐÁÙ½çÇø´úÂ룬¿ÉÄÜ»á²úÉú´íÎó¡£Í¨³£ÒªÇó`Thread1`Ö´ÐÐÍêÁÙ½çÇø´úÂëºó£¬`Thread2`²ÅÄÜÖ´ÐÐÁÙ½çÇø´úÂë¡£ + +µ±Á½¸ö»ò¶à¸öÏß³ÌÐèÒª·ÃÎÊͬһ×ÊԴʱ£¬ËüÃÇÐèÒªÒÔijÖÖ˳ÐòÀ´È·±£¸Ã×ÊԴijһʱ¿ÌÖ»Äܱ»Ò»¸öÏß³ÌʹÓõķ½Ê½³ÆÎª **Ïß³Ìͬ²½**¡£ + + +¡¾Àý×Ó¡¿Óй²Ïí×ÊÔ´µÄÇé¿öϲ»Í¬²½µÄÀý×Ó¡£ + +¹²Ïí×ÊÔ´Àࣺ +```c +public class Resource +{ + private int context = 0; + public void Write() + { + context++; + Console.WriteLine("Writer:{0}", context); + } + public void Read() + { + Console.WriteLine("Reader{0}", context); + } +} +``` +дÏß³ÌÖ´ÐеÄÀࣺ +```c +public class Writer +{ + private Resource rec; + public Writer(Resource r) + { + rec = r; + } + public void Run() + { + for (int i = 0; i < 10; i++) + rec.Write(); + } +} +``` + +¶ÁÏß³ÌÖ´ÐеÄÀࣺ +```c +public class Reader +{ + private Resource rec; + public Reader(Resource r) + { + rec = r; + } + public void Run() + { + for (int i = 0; i < 10; i++) + rec.Read(); + } +} +``` + +¿Í»§¶Ë´úÂ룺 +```c +public class Program +{ + static void Main(string[] args) + { + Resource r = new Resource(); + Writer writer1 = new Writer(r); + Reader reader1 = new Reader(r); + + Thread rThread = new Thread(writer1.Run); + Thread wThread = new Thread(reader1.Run); + + wThread.Start(); + rThread.Start(); + } +} +``` + +![ͼ15 ÔËÐнá¹û](https://img-blog.csdnimg.cn/20201009105256873.png) + + +- ÀûÓÃ`lock`¹Ø¼ü×Ö½øÐÐÏß³Ìͬ²½¡£ + +Ó÷¨£º + +```c +private static object obj = new object(); +public void Funciton() +{ + lock (obj) + { + //ÐèҪͬ²½µÄ´úÂë¶Î + } +} +``` + +¸Ä½ø£º +```c +public class Resource +{ + private int context = 0; + private static object obj = new object(); + public void Write() + { + lock (obj) + { + context++; + Console.WriteLine("Writer:{0}", context); + } + } + public void Read() + { + lock (obj) + { + Console.WriteLine("Reader{0}", context); + } + } +} +``` +![ͼ16 ÔËÐнá¹û](https://img-blog.csdnimg.cn/20201009110833454.png) + +- ÀûÓÃ`Monitor`Àà½øÐÐÏß³Ìͬ²½¡£ + +Ó÷¨£º + +```c +private static object obj = new object(); +public void Funtion() +{ + Monitor.Enter(obj);//ÔÚÖ¸¶¨¶ÔÏóÉÏ»ñÈ¡ÅÅËûËø¡£ + //ÐèҪͬ²½µÄ´úÂë¶Î + Monitor.Exit(obj);//ÊÍ·ÅÖ¸¶¨¶ÔÏóÉϵÄÅÅËûËø¡£ +} +``` + +¸Ã½á¹¹Óë`lock`¹Ø¼ü×ֽṹµÈ¼Û¡£ +```c +public class Resource +{ + private int context = 0; + private static object obj = new object(); + public void Write() + { + Monitor.Enter(obj); + context++; + Console.WriteLine("Writer:{0}", context); + Monitor.Exit(obj); + } + public void Read() + { + Monitor.Enter(obj); + Console.WriteLine("Reader{0}", context); + Monitor.Exit(obj); + } +} +``` + +![ͼ17 ÔËÐнá¹û](https://img-blog.csdnimg.cn/20201009110833454.png) + +¡¾Àý×Ó¡¿`wThread`Ïß³ÌдÈëÊý¾Ýºó£¬`rThread`Ï̶߳ÁÈ¡Êý¾Ý£¬½»Ìæ½øÐС£ + +```c +public class Resource +{ + private int context = 0; + private bool readFlag = false; + public void Write() + { + while (readFlag == true)//true:Ö»ÄܶÁ,²»ÄÜд(´¦ÓÚ¶Á״̬). + ; + context++; + Console.WriteLine("Writer:{0}", context); + readFlag = true; + } + public void Read() + { + while (readFlag == false)//false:Ö»ÄÜд,²»ÄܶÁ(´¦ÓÚд״̬). + ; + Console.WriteLine("Reader{0}", context); + readFlag = false; + } +} +``` + +![ͼ18 ÔËÐнá¹û](https://img-blog.csdnimg.cn/20201009112823798.png) + + + + + + + + + + + +ÒÔÏ´úÂëÓëÉÏÃæ´úÂëµÈ¼Û£º +```c +public class Resource +{ + private int context = 0; + private bool readFlag = false; + private static object obj = new object(); + + public void Write() + { + Monitor.Enter(obj); + if (readFlag == true)//true:Ö»ÄܶÁ,²»ÄÜд(´¦ÓÚ¶Á״̬). + Monitor.Wait(obj);//Ï߳̽øÈë¶ÂÈû¶ÓÁÐ + + context++; + Console.WriteLine("Writer:{0}", context); + readFlag = true; + + Monitor.Pulse(obj);//´Ó¶ÂÈû¶ÓÁÐÖгö¶Ó + Monitor.Exit(obj); + } + + public void Read() + { + Monitor.Enter(obj); + if (readFlag == false)//false:Ö»ÄÜд,²»ÄܶÁ(´¦ÓÚд״̬). + Monitor.Wait(obj); + + Console.WriteLine("Reader:{0}", context); + readFlag = false; + + Monitor.Pulse(obj); + Monitor.Exit(obj); + } +} +``` + +![ͼ19 ÔËÐнá¹û](https://img-blog.csdnimg.cn/20201009112823798.png) + + + +--- +## 6. ¶ÓÁеÄÓ¦Óà + +Ŀǰ£¬ÔÚÒÔÒøÐÐÓªÒµ´óÌüΪ´ú±íµÄ´°¿ÚÐÐÒµÖдóÁ¿Ê¹ÓÃÅŶӣ¨½ÐºÅ£©ÏµÍ³£¬¸ÃϵͳÍêȫģÄâÁËÈËȺÅŶÓÈ«¹ý³Ì£¬Í¨¹ýȡƱ½ø¶Ó¡¢ÅŶӵȴý¡¢½ÐºÅ·þÎñµÈ¹¦ÄÜ£¬´úÌæÁËÈËÃÇÕ¾¶ÓµÄÐÁ¿à¡£ + +**1. ÎÊÌâÃèÊö** + +ÅŶӽкÅÈí¼þµÄ¾ßÌå²Ù×÷Á÷³ÌΪ£º + +- ¹Ë¿ÍÈ¡·þÎñÐòºÅ + +µ±¹Ë¿ÍµÖ´ï·þÎñ´óÌüʱ£¬Ç°Íù·ÅÖÃÔÚÈë¿Ú´¦ÅÔµÄÈ¡ºÅ»ú£¬²¢°´Ò»ÏÂÆäÉϵÄÏàÓ¦·þÎñ°´Å¥£¬È¡ºÅ»ú»á×Ô¶¯´òÓ¡³öÒ»ÕÅ·þÎñµ¥¡£µ¥ÉÏÏÔʾ·þÎñºÅ¼°¸Ã·þÎñºÅÇ°ÃæÕýÔڵȴý·þÎñµÄÈËÊý¡£ + +- ·þÎñÔ±¹¤ºô½Ð¹Ë¿Í + +·þÎñÔ±¹¤Ö»Ðè°´Ò»ÏÂÆä¹ñ̨ÉϺô½ÐÆ÷µÄÏàÓ¦°´Å¥£¬Ôò¹Ë¿ÍµÄ·þÎñºÅ¾Í»á°´Ë³ÐòµÄÏÔʾÔÚÏÔʾÆÁÉÏ£¬²¢·¢³ö¡°¶£ßË¡±ºÍÏà¹ØÓïÒôÐÅÏ¢£¬Ìáʾ¹Ë¿ÍǰÍù¸Ã´°¿Ú°ìÊ¡£µ±Ò»Î»¹Ë¿Í°ìÊÂÍê±Ïºó£¬¹ñ̨·þÎñÔ±¹¤Ö»Ðè°´ºô½ÐÆ÷ÏàÓ¦¼ü£¬¼´¿É×Ô¶¯ºô½ÐÏÂһλ¹Ë¿Í¡£ + +**2. ÎÊÌâ·ÖÎö** + +±àд³ÌÐòÄ£ÄâÉÏÃæµÄ¹¤×÷¹ý³Ì£¬Ö÷ÒªÒªÇóÈçÏ£º + +- ³ÌÐòÔËÐк󣬵±¿´µ½¡°Çëµã»÷´¥ÃþÆÁ»ñÈ¡ºÅÂ룺¡±µÄÌáʾʱ£¬Ö»Òª°´»Ø³µ¼ü£¬¼´¿ÉÏÔʾ¡°ÄúµÄºÅÂëÊÇ£ºXXX£¬ÄúÇ°ÃæÓÐYYYλ¡±µÄÌáʾ£¬ÆäÖÐXXXÊÇËù»ñµÃµÄ·þÎñºÅÂ룬YYYÊÇÔÚXXX֮ǰÀ´µ½µÄÕýÔڵȴý·þÎñµÄÈËÊý¡£ +- ÓöàÏ̼߳¼ÊõÄ£Äâ·þÎñ´°¿Ú£¨¿ÉÄ£Äâ¶à¸ö£©£¬¾ßÓзþÎñÔ±ºô½Ð¹Ë¿ÍµÄÐÐΪ£¬¼ÙÉèÿ¸ö¹Ë¿Í·þÎñµÄʱ¼äÊÇ5000ms£¬Ê±¼äµ½ºó£¬ÏÔʾ¡°ÇëXXXºÅµ½ZZZºÅ´°¿Ú£¡¡±µÄÌáʾ¡£ÆäÖÐZZZÊǼ´½«Îª¿Í»§·þÎñµÄ´°¿ÚºÅ¡£ + + +**3. ´úÂëʵÏÖ** + +```c +using System.Threading; +using LinearStruct; + +namespace BankQueue +{ + public class BankQueue : LinkQueue + { + public int Callnumber { get; private set; } + public int GetCallnumber() + { + Callnumber++; + return Callnumber; + } + + public BankQueue() + { + Callnumber = 0; + } + } + + public class ServiceWindow + { + private BankQueue _bankQueue; + public ServiceWindow(BankQueue bankQueue) + { + _bankQueue = bankQueue; + } + public void Service() + { + while (true) + { + lock (_bankQueue) + { + Thread.Sleep(5000); + if (_bankQueue.Length != 0) + { + Console.WriteLine(); + Console.WriteLine("Çë{0}ºÅµ½{1}ºÅ´°¿Ú!", + _bankQueue.QueueFront, Thread.CurrentThread.Name); + _bankQueue.DeQueue(); + } + } + } + } + } +} +``` + +¿Í»§¶Ë´úÂ룺 + +```c +class Program +{ + public static void Main(string[] args) + { + int windowCount = 3; + BankQueue bankQueue = new BankQueue(); + ServiceWindow[] serviceWindows = new ServiceWindow[windowCount]; + Thread[] serviceThread = new Thread[windowCount]; + for (int i = 0; i < windowCount; i++) + { + serviceWindows[i] = new ServiceWindow(bankQueue); + serviceThread[i] = new Thread(serviceWindows[i].Service) + { + Name = (i + 1).ToString() + }; + serviceThread[i].Start(); + } + while (true) + { + Console.WriteLine("Çëµã»÷´¥ÃþÆÁ»ñÈ¡ºÅÂ룺"); + Console.ReadLine(); + int callnumber = bankQueue.GetCallnumber(); + Console.WriteLine("ÄúµÄºÅÂëÊÇ£º{0}£¬ÄãÇ°ÃæÓÐ{1}룬ÇëµÈ´ý£¡", + callnumber, bankQueue.Length); + bankQueue.EnQueue(callnumber); + } + } +} +``` + +**4. ½á¹ûÏÔʾ** + +![ͼ20 Ä£Äâ½á¹û](https://img-blog.csdnimg.cn/20201008210053215.png) +