资源简介
爸爸放苹果,妈妈放橘子,盘子只能容纳两个水果。儿子只吃苹果,女儿只吃橘子。用信号量控制
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApplication1.Properties;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private static int[] buffer = new int[2];//缓冲区数组 0表示空,1表示苹果,2表示橘子
Thread t1 = null;
Thread t2 = null;
Thread t3 = null;
Thread t4 = null;
private static Mutex mutex = new Mutex();
private static Semaphore sempty = new Semaphore(2 2); //剩余空位的信号量
private static Semaphore dorange = new Semaphore(0 2); //橘子个数的信号量
private static Semaphore sapple = new Semaphore(0 2); //苹果个数的信号量
public Form1()
{
InitializeComponent();
Form1.CheckForIllegalCrossThreadCalls = false;
}
private void Form1_Load(object sender EventArgs e)
{
//让控件不闪烁
orange.Hide();
apple.Hide();
pictureBox2.Hide();
pictureBox1.Hide();
//创建four个线程并启动
}
public void ThreadProductOrange()
{
while(true)
{
this.Setstyle(Controlstyles.OptimizedDoubleBuffer | Controlstyles.ResizeRedraw | Controlstyles.AllPaintingInWmPaint true);
mother.Location = new Point(this.mother.Location.X - 1 this.mother.Location.Y + 1);
if (this.mother.Location.Y== 203)
{
sempty.WaitOne();
mutex.WaitOne();
if (buffer[0] == 0)
{
buffer[0] = 1;
orange.Show();
mutex.ReleaseMutex();
dorange.Release();
}
else if (buffer[1] == 0)
{
buffer[1] = 1;
pictureBox2.Show();
mutex.ReleaseMutex();
dorange.Release();
}
}
if (this.mother.Location.Y >= 203)
{
mother.Location = new Point(525 53);
}
Thread.Sleep(30);
}
}
public void ThreadProductApple()
{
while (true)
{
this.Setstyle(Controlstyles.OptimizedDoubleBuffer | Controlstyles.ResizeRedraw | Controlstyles.AllPaintingInWmPaint true);
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2016-11-15 21:04 WindowsFormsApplication1\
目录 0 2016-11-15 21:04 WindowsFormsApplication1\.vs\
目录 0 2016-11-15 21:04 WindowsFormsApplication1\.vs\WindowsFormsApplication1\
目录 0 2016-11-15 21:04 WindowsFormsApplication1\.vs\WindowsFormsApplication1\v14\
文件 74752 2016-11-14 23:08 WindowsFormsApplication1\.vs\WindowsFormsApplication1\v14\.suo
目录 0 2016-11-15 21:04 WindowsFormsApplication1\WindowsFormsApplication1\
文件 1039 2016-11-01 21:01 WindowsFormsApplication1\WindowsFormsApplication1.sln
文件 8180 2016-11-01 21:13 WindowsFormsApplication1\WindowsFormsApplication1\235013-14032Q05P815.jpg
文件 189 2016-11-01 21:01 WindowsFormsApplication1\WindowsFormsApplication1\App.config
目录 0 2016-11-15 21:04 WindowsFormsApplication1\WindowsFormsApplication1\bin\
目录 0 2016-11-15 21:04 WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\
文件 2064896 2016-11-15 21:03 WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe
文件 189 2016-11-01 21:01 WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe.config
文件 36352 2016-11-15 21:03 WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.pdb
文件 22696 2016-11-15 20:54 WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.vshost.exe
文件 189 2016-11-01 21:01 WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.vshost.exe.config
文件 490 2012-06-06 02:06 WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.vshost.exe.manifest
目录 0 2016-11-12 17:41 WindowsFormsApplication1\WindowsFormsApplication1\bin\Release\
文件 8293 2016-11-15 21:02 WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs
文件 10109 2016-11-15 21:02 WindowsFormsApplication1\WindowsFormsApplication1\Form1.Designer.cs
文件 105700 2016-11-15 21:02 WindowsFormsApplication1\WindowsFormsApplication1\Form1.resx
文件 628 2016-11-12 18:52 WindowsFormsApplication1\WindowsFormsApplication1\GoodsApple.cs
文件 759 2016-11-12 18:52 WindowsFormsApplication1\WindowsFormsApplication1\GoodsOrange.cs
目录 0 2016-11-15 21:04 WindowsFormsApplication1\WindowsFormsApplication1\obj\
目录 0 2016-11-15 21:04 WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\
文件 1464 2016-11-02 16:20 WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\DesignTimeResolveAssemblyReferences.cache
文件 8577 2016-11-12 18:31 WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 0 2016-11-01 21:01 WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
文件 0 2016-11-01 21:01 WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
文件 0 2016-11-01 21:01 WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
目录 0 2016-11-15 21:04 WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\TempPE\
............此处省略25个文件信息
- 上一篇:测试与计量技术基础 周渭
- 下一篇:Smart AI Car
评论
共有 条评论