資源簡介
爸爸放蘋果,媽媽放橘子,盤子只能容納兩個水果。兒子只吃蘋果,女兒只吃橘子。用信號量控制

代碼片段和文件信息
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
評論
共有 條評論