有关水果问题的信号量程序
爸爸放苹果,妈妈放橘子,盘子只能容纳两个水果。儿子只吃苹果,女儿只吃橘子。用信号量控制
代码片段和文件信息
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2016-11-15 21:04 WindowsFormsApplication1
目录 0 2016-11-15 21:04 WindowsFormsApplication1.vs
目录 0 2016-11-15 21:04 WindowsFormsApplication1.vsWindowsFormsApplication1
目录 0 2016-11-15 21:04 WindowsFormsApplication1.vsWindowsFormsApplication1v14
文件 74752 2016-11-14 23:08 WindowsFormsApplication1.vsWindowsFormsApplication1v14.suo
目录 0 2016-11-15 21:04 WindowsFormsApplication1WindowsFormsApplication1
文件 1039 2016-11-01 21:01 WindowsFormsApplication1WindowsFormsApplication1.sln
文件 8180 2016-11-01 21:13 WindowsFormsApplication1WindowsFormsApplication1235013-14032Q05P815.jpg
文件 189 2016-11-01 21:01 WindowsFormsApplication1WindowsFormsApplication1App.config
目录 0 2016-11-15 21:04 WindowsFormsApplication1WindowsFormsApplication1in
目录 0 2016-11-15 21:04 WindowsFormsApplication1WindowsFormsApplication1inDebug
文件 2064896 2016-11-15 21:03 WindowsFormsApplication1WindowsFormsApplication1inDebugWindowsFormsApplication1.exe
文件 189 2016-11-01 21:01 WindowsFormsApplication1WindowsFormsApplication1inDebugWindowsFormsApplication1.exe.config
文件 36352 2016-11-15 21:03 WindowsFormsApplication1WindowsFormsApplication1inDebugWindowsFormsApplication1.pdb
文件 22696 2016-11-15 20:54 WindowsFormsApplication1WindowsFormsApplication1inDebugWindowsFormsApplication1.vshost.exe
文件 189 2016-11-01 21:01 WindowsFormsApplication1WindowsFormsApplication1inDebugWindowsFormsApplication1.vshost.exe.config
文件 490 2012-06-06 02:06 WindowsFormsApplication1WindowsFormsApplication1inDebugWindowsFormsApplication1.vshost.exe.manifest
目录 0 2016-11-12 17:41 WindowsFormsApplication1WindowsFormsApplication1inRelease
文件 8293 2016-11-15 21:02 WindowsFormsApplication1WindowsFormsApplication1Form1.cs
文件 10109 2016-11-15 21:02 WindowsFormsApplication1WindowsFormsApplication1Form1.Designer.cs
文件 105700 2016-11-15 21:02 WindowsFormsApplication1WindowsFormsApplication1Form1.resx
文件 628 2016-11-12 18:52 WindowsFormsApplication1WindowsFormsApplication1GoodsApple.cs
文件 759 2016-11-12 18:52 WindowsFormsApplication1WindowsFormsApplication1GoodsOrange.cs
目录 0 2016-11-15 21:04 WindowsFormsApplication1WindowsFormsApplication1obj
目录 0 2016-11-15 21:04 WindowsFormsApplication1WindowsFormsApplication1objDebug
文件 1464 2016-11-02 16:20 WindowsFormsApplication1WindowsFormsApplication1objDebugDesignTimeResolveAssemblyReferences.cache
文件 8577 2016-11-12 18:31 WindowsFormsApplication1WindowsFormsApplication1objDebugDesignTimeResolveAssemblyReferencesInput.cache
文件 0 2016-11-01 21:01 WindowsFormsApplication1WindowsFormsApplication1objDebugTemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
文件 0 2016-11-01 21:01 WindowsFormsApplication1WindowsFormsApplication1objDebugTemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
文件 0 2016-11-01 21:01 WindowsFormsApplication1WindowsFormsApplication1objDebugTemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
目录 0 2016-11-15 21:04 WindowsFormsApplication1WindowsFormsApplication1objDebugTempPE
............此处省略25个文件信息
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.vsWindowsFormsApplication1
目录 0 2016-11-15 21:04 WindowsFormsApplication1.vsWindowsFormsApplication1v14
文件 74752 2016-11-14 23:08 WindowsFormsApplication1.vsWindowsFormsApplication1v14.suo
目录 0 2016-11-15 21:04 WindowsFormsApplication1WindowsFormsApplication1
文件 1039 2016-11-01 21:01 WindowsFormsApplication1WindowsFormsApplication1.sln
文件 8180 2016-11-01 21:13 WindowsFormsApplication1WindowsFormsApplication1235013-14032Q05P815.jpg
文件 189 2016-11-01 21:01 WindowsFormsApplication1WindowsFormsApplication1App.config
目录 0 2016-11-15 21:04 WindowsFormsApplication1WindowsFormsApplication1in
目录 0 2016-11-15 21:04 WindowsFormsApplication1WindowsFormsApplication1inDebug
文件 2064896 2016-11-15 21:03 WindowsFormsApplication1WindowsFormsApplication1inDebugWindowsFormsApplication1.exe
文件 189 2016-11-01 21:01 WindowsFormsApplication1WindowsFormsApplication1inDebugWindowsFormsApplication1.exe.config
文件 36352 2016-11-15 21:03 WindowsFormsApplication1WindowsFormsApplication1inDebugWindowsFormsApplication1.pdb
文件 22696 2016-11-15 20:54 WindowsFormsApplication1WindowsFormsApplication1inDebugWindowsFormsApplication1.vshost.exe
文件 189 2016-11-01 21:01 WindowsFormsApplication1WindowsFormsApplication1inDebugWindowsFormsApplication1.vshost.exe.config
文件 490 2012-06-06 02:06 WindowsFormsApplication1WindowsFormsApplication1inDebugWindowsFormsApplication1.vshost.exe.manifest
目录 0 2016-11-12 17:41 WindowsFormsApplication1WindowsFormsApplication1inRelease
文件 8293 2016-11-15 21:02 WindowsFormsApplication1WindowsFormsApplication1Form1.cs
文件 10109 2016-11-15 21:02 WindowsFormsApplication1WindowsFormsApplication1Form1.Designer.cs
文件 105700 2016-11-15 21:02 WindowsFormsApplication1WindowsFormsApplication1Form1.resx
文件 628 2016-11-12 18:52 WindowsFormsApplication1WindowsFormsApplication1GoodsApple.cs
文件 759 2016-11-12 18:52 WindowsFormsApplication1WindowsFormsApplication1GoodsOrange.cs
目录 0 2016-11-15 21:04 WindowsFormsApplication1WindowsFormsApplication1obj
目录 0 2016-11-15 21:04 WindowsFormsApplication1WindowsFormsApplication1objDebug
文件 1464 2016-11-02 16:20 WindowsFormsApplication1WindowsFormsApplication1objDebugDesignTimeResolveAssemblyReferences.cache
文件 8577 2016-11-12 18:31 WindowsFormsApplication1WindowsFormsApplication1objDebugDesignTimeResolveAssemblyReferencesInput.cache
文件 0 2016-11-01 21:01 WindowsFormsApplication1WindowsFormsApplication1objDebugTemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
文件 0 2016-11-01 21:01 WindowsFormsApplication1WindowsFormsApplication1objDebugTemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
文件 0 2016-11-01 21:01 WindowsFormsApplication1WindowsFormsApplication1objDebugTemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
目录 0 2016-11-15 21:04 WindowsFormsApplication1WindowsFormsApplication1objDebugTempPE
............此处省略25个文件信息
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。
评论列表(条)