C#中一道關(guān)于多線(xiàn)程的基礎(chǔ)練習(xí)題——模擬倉(cāng)庫(kù)存銷(xiāo)過(guò)程
題目:模擬生產(chǎn)、入庫(kù)、銷(xiāo)售(50分)
假設(shè)某企業(yè)自產(chǎn)、自存、自銷(xiāo),需要將工廠生產(chǎn)的各類(lèi)產(chǎn)品不定時(shí)的運(yùn)到倉(cāng)庫(kù),與此同時(shí),需要將倉(cāng)庫(kù)中的貨物運(yùn)往超市和商場(chǎng)中進(jìn)行銷(xiāo)售,請(qǐng)編寫(xiě)一個(gè)程序模擬此過(guò)程(主要是存取這個(gè)過(guò)程)。
評(píng)分標(biāo)準(zhǔn):
1. 倉(cāng)庫(kù)的存量是固定的,可以假設(shè)為一個(gè)常量,比如10。(5分)
2. 倉(cāng)庫(kù)滿(mǎn)的時(shí)候,不能再向倉(cāng)庫(kù)中存貨。(10分)
3. 倉(cāng)庫(kù)空的時(shí)候,不能賣(mài)出貨物。(10分)
4. 存貨和取貨是同時(shí)進(jìn)行的,不要出現(xiàn)先存滿(mǎn)再取完貨再存滿(mǎn)再取完的效果或者存一個(gè)取一個(gè)再存再取這樣的效果。(15分)
5. 思路清晰,輸出工整,編碼規(guī)范,有正確的異常處理。(10分)
用多線(xiàn)程模擬倉(cāng)庫(kù)存儲(chǔ)和銷(xiāo)售的過(guò)程代碼如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- using System.IO;
- namespace MultiThreadStore
- {
- class Program
- {
- //入口
- static void Main(string[] args)
- {
- Goods goods = new Goods();
- Thread storeGoods = new Thread(new ParameterizedThreadStart(store));
- Thread sellGoods = new Thread(new ParameterizedThreadStart(sell));
- storeGoods.Start(goods);
- sellGoods.Start(goods);
- Console.ReadLine();
- }
- //存貨方法
- private static void store(object obj)
- {
- bool storeFlag = true;
- Random random = new Random();
- while (storeFlag)
- {
- try
- {
- Goods goods = obj as Goods;
- if (goods.Num < goods.MaxNum)
- {
- goods.Num++;
- Console.WriteLine("Store a goods, " + goods.Num + " goods left!");
- }
- else
- {
- Console.WriteLine("The store is full now.");
- }
- Thread.Sleep(random.Next(500, 1000));
- }
- catch (Exception ex)
- {
- WriteLog(ex);
- storeFlag = false;
- }
- }
- }
- //賣(mài)貨方法
- public static void sell(object obj)
- {
- bool sellFlag = true;
- Random random = new Random();
- while (sellFlag)
- {
- try
- {
- Goods goods = obj as Goods;
- if (goods.Num > 0)
- {
- goods.Num--;
- Console.WriteLine("Sell a goods, " + goods.Num + " goods left!");
- }
- else
- {
- Console.WriteLine("There are no goods now.");
- }
- Thread.Sleep(random.Next(1000, 4000));
- }
- catch (Exception ex)
- {
- WriteLog(ex);
- sellFlag = false;
- }
- }
- }
- //打log方法
- private static void WriteLog(Exception ex)
- {
- string logUrl = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\MuliThreadStorelog.txt";
- if (File.Exists(@logUrl))
- {
- using (FileStream fs = new FileStream(logUrl, FileMode.Append))
- {
- using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
- {
- try
- {
- sw.Write(ex);
- }
- catch (Exception ex1)
- {
- WriteLog(ex1);
- }
- finally
- {
- sw.Close();
- fs.Close();
- }
- }
- }
- }
- else
- {
- using (FileStream fs = new FileStream(logUrl, FileMode.CreateNew))
- {
- using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
- {
- try
- {
- sw.Write(ex);
- }
- catch (Exception ex1)
- {
- WriteLog(ex1);
- }
- finally
- {
- sw.Close();
- fs.Close();
- }
- }
- }
- }
- }
- }
- //貨品類(lèi)
- class Goods
- {
- public int Num { get; set; }
- public int MaxNum { get; set; }
- public Goods()
- {
- Num = 10;
- MaxNum = 50;
- }
- }
- }