• 大小: 17KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-16
  • 语言: C#
  • 标签:

资源简介

哲学家用餐问题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


评论

共有 条评论

相关资源