資源簡介
哲學家用餐問題C#實現,源碼可以運行,多線程互斥操作

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Text;
using?System.Windows.Forms;
using?System.Threading;?
namespace?DiningPhilosophers
{
????public?partial?class?Form1?:?Form
????{
????????private?Philosopher[]?p?=?new?Philosopher[5];?
????????public?Form1()
????????{
????????????InitializeComponent();
????????????Mutex[]?chopSticks?=?new?Mutex[5];
????????????for?(int?i?=?0;?i?5;?i++)
????????????{
????????????????chopSticks[i]?=?new?Mutex(false);
????????????}
????????????for?(int?i?=?0;?i?5;?i++)
????????????{
????????????????PhilosopherData?pd;
????????????????pd.PhilosopherId?=?i;
????????????????pd.RightChopStick?=?chopSticks[(i?+?1)?%?5];
????????????????pd.LeftChopStick?=?chopSticks[(i?+?4)?%?5];
????????????????pd.AmountToEat?=?5;
????????????????pd.TotalFood?=?35;
????????????????p[i]?=?new?Philosopher(pd);
????????????????p[i].MessageArrival?+=?new?Philosopher.MessageArrivedHandler(ShowMessage);
????????????}?
????????}
????????
????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
????????????System.Windows.Forms.Form.CheckForIllegalCrossThreadCalls?=?false;
????????}
????????private?void?button1_Click(object?sender?EventArgs?e)
????????{
????????????for?(int?i?=?0;?i?5;?i++)
????????????????p[i].Start();?
????????}
????????public?void?ShowMessage(object?sender?MessageArrivedEventArgs?e)
????????{
????????????switch?(e.type)
????????????{
????????????????case?Philosopher.READY:
????????????????????listBox1.Items.Add(“Philosopher(“?+?e.philosopherData.PhilosopherId?+?“)?ready.“);
????????????????????break;
????????????????case?Philosopher.EATING:
????????????????????listBox1.Items.Add(“Philosopher(“?+
????????????????????e.philosopherData.PhilosopherId?+?“)?eating?“?+
????????????????????e.philosopherData.AmountToEat?+?“?of?“?+
????????????????????e.philosopherData.TotalFood?+?“?food.“);
????????????????????break;
????????????????case?Philosopher.THINKING:
????????????????????listBox1.Items.Add(“Philosopher(“?+?e.philosopherData.PhilosopherId?+?“)?thinking.“);
????????????????????break;
????????????????case?Philosopher.FINISHED:
????????????????????listBox1.Items.Add(“Philosopher(“?+?e.philosopherData.PhilosopherId?+?“)?finished.“);
????????????????????break;
????????????}
????????}
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????5632??2005-12-08?14:51??DiningPhilosophers\DiningPhilosophers\bin\Debug\DiningPhilosophers.vshost.exe
?????文件???????3328??2010-01-14?16:49??DiningPhilosophers\DiningPhilosophers\DiningPhilosophers.csproj
?????文件???????2415??2010-01-14?17:03??DiningPhilosophers\DiningPhilosophers\Form1.cs
?????文件???????2651??2010-01-14?16:58??DiningPhilosophers\DiningPhilosophers\Form1.Designer.cs
?????文件???????5814??2010-01-14?16:58??DiningPhilosophers\DiningPhilosophers\Form1.resx
?????文件???????2468??2010-01-14?16:57??DiningPhilosophers\DiningPhilosophers\Philosopher.cs
?????文件????????477??2010-01-14?16:41??DiningPhilosophers\DiningPhilosophers\Program.cs
?????文件???????1188??2010-01-14?16:41??DiningPhilosophers\DiningPhilosophers\Properties\AssemblyInfo.cs
?????文件???????2892??2010-01-14?16:41??DiningPhilosophers\DiningPhilosophers\Properties\Resources.Designer.cs
?????文件???????5612??2010-01-14?16:41??DiningPhilosophers\DiningPhilosophers\Properties\Resources.resx
?????文件???????1103??2010-01-14?16:41??DiningPhilosophers\DiningPhilosophers\Properties\Settings.Designer.cs
?????文件????????249??2010-01-14?16:41??DiningPhilosophers\DiningPhilosophers\Properties\Settings.settings
?????文件???????1060??2010-01-14?16:51??DiningPhilosophers\DiningPhilosophers\WorkerThread.cs
?????文件???????1093??2010-01-14?16:41??DiningPhilosophers\DiningPhilosophers.sln
????..A..H.?????15872??2010-01-14?17:05??DiningPhilosophers\DiningPhilosophers.suo
?????目錄??????????0??2010-01-14?16:41??DiningPhilosophers\DiningPhilosophers\obj\Debug\TempPE
?????目錄??????????0??2010-01-14?17:05??DiningPhilosophers\DiningPhilosophers\bin\Debug
?????目錄??????????0??2010-01-14?17:01??DiningPhilosophers\DiningPhilosophers\obj\Debug
?????目錄??????????0??2010-01-14?16:41??DiningPhilosophers\DiningPhilosophers\bin
?????目錄??????????0??2010-01-14?17:01??DiningPhilosophers\DiningPhilosophers\obj
?????目錄??????????0??2010-01-14?16:41??DiningPhilosophers\DiningPhilosophers\Properties
?????目錄??????????0??2010-01-14?17:03??DiningPhilosophers\DiningPhilosophers
?????目錄??????????0??2010-01-14?16:41??DiningPhilosophers
-----------?---------??----------?-----??----
????????????????51854????????????????????23
評論
共有 條評論