C#中一道关于多线程的基础练习题——模拟仓库存销过程

题目:模拟生产、入库、销售(50分)

创新互联公司是一家集成都网站设计、成都做网站、外贸网站建设、网站页面设计、网站优化SEO优化为一体的专业网站制作公司,已为成都等多地近百家企业提供网站建设服务。追求良好的浏览体验,以探求精品塑造与理念升华,设计最适合用户的网站页面。 合作只是第一步,服务才是根本,我们始终坚持讲诚信,负责任的原则,为您进行细心、贴心、认真的服务,与众多客户在蓬勃发展的市场环境中,互促共生。

 假设某企业自产、自存、自销,需要将工厂生产的各类产品不定时的运到仓库,与此同时,需要将仓库中的货物运往超市和商场中进行销售,请编写一个程序模拟此过程(主要是存取这个过程)。

评分标准:

1. 仓库的存量是固定的,可以假设为一个常量,比如10。(5分)

2. 仓库满的时候,不能再向仓库中存货。(10分)

3. 仓库空的时候,不能卖出货物。(10分)

4. 存货和取货是同时进行的,不要出现先存满再取完货再存满再取完的效果或者存一个取一个再存再取这样的效果。(15分)

5. 思路清晰,输出工整,编码规范,有正确的异常处理。(10分)

用多线程模拟仓库存储和销售的过程代码如下:

 
 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Threading;
  7. using System.IO;
  8. namespace MultiThreadStore
  9. {
  10.     class Program
  11.     {
  12.         //入口
  13.         static void Main(string[] args)
  14.         {
  15.             Goods goods = new Goods();
  16.             Thread storeGoods = new Thread(new ParameterizedThreadStart(store));
  17.             Thread sellGoods = new Thread(new ParameterizedThreadStart(sell));
  18.             storeGoods.Start(goods);
  19.             sellGoods.Start(goods);
  20.             Console.ReadLine();
  21.         }
  22.         //存货方法
  23.         private static void store(object obj)
  24.         {
  25.             bool storeFlag = true;
  26.             Random random = new Random();
  27.             while (storeFlag)
  28.             {
  29.                 try
  30.                 {
  31.                     Goods goods = obj as Goods;
  32.                     if (goods.Num < goods.MaxNum)
  33.                     {
  34.                         goods.Num++;
  35.                         Console.WriteLine("Store a goods, " + goods.Num + " goods left!");
  36.                     }
  37.                     else 
  38.                     {
  39.                         Console.WriteLine("The store is full now.");
  40.                     }
  41.                     Thread.Sleep(random.Next(500, 1000));
  42.                 }
  43.                 catch (Exception ex)
  44.                 {
  45.                     WriteLog(ex);
  46.                     storeFlag = false;
  47.                 }
  48.             }
  49.         }
  50.         //卖货方法
  51.         public static void sell(object obj) 
  52.         {
  53.             bool sellFlag = true;
  54.             Random random = new Random();
  55.             while (sellFlag)
  56.             {
  57.                 try
  58.                 {
  59.                     Goods goods = obj as Goods;
  60.                     if (goods.Num > 0)
  61.                     {
  62.                         goods.Num--;
  63.                         Console.WriteLine("Sell a goods, " + goods.Num + " goods left!");
  64.                     }
  65.                     else 
  66.                     {
  67.                         Console.WriteLine("There are no goods now.");
  68.                     }
  69.                     Thread.Sleep(random.Next(1000, 4000));
  70.                 }
  71.                 catch (Exception ex)
  72.                 {
  73.                     WriteLog(ex);
  74.                     sellFlag = false;
  75.                 }
  76.             }
  77.         }
  78.         //打log方法
  79.         private static void WriteLog(Exception ex)
  80.         {
  81.             string logUrl = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\MuliThreadStorelog.txt";
  82.             if (File.Exists(@logUrl))
  83.             {
  84.                 using (FileStream fs = new FileStream(logUrl, FileMode.Append))
  85.                 {
  86.                     using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
  87.                     {
  88.                         try
  89.                         {
  90.                             sw.Write(ex);
  91.                         }
  92.                         catch (Exception ex1)
  93.                         {
  94.                             WriteLog(ex1);
  95.                         }
  96.                         finally
  97.                         {
  98.                             sw.Close();
  99.                             fs.Close();
  100.                         }
  101.                     }
  102.                 }
  103.             }
  104.             else
  105.             {
  106.                 using (FileStream fs = new FileStream(logUrl, FileMode.CreateNew))
  107.                 {
  108.                     using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
  109.                     {
  110.                         try
  111.                         {
  112.                             sw.Write(ex);
  113.                         }
  114.                         catch (Exception ex1)
  115.                         {
  116.                             WriteLog(ex1);
  117.                         }
  118.                         finally
  119.                         {
  120.                             sw.Close();
  121.                             fs.Close();
  122.                         }
  123.                     }
  124.                 }
  125.             }
  126.         }
  127.     }
  128.     //货品类
  129.     class Goods 
  130.     {
  131.         public int Num { get; set; }
  132.         public int MaxNum { get; set; }
  133.         public Goods() 
  134.         {
  135.             Num = 10;
  136.             MaxNum = 50;
  137.         }     
  138.     }
  139. }

运行截图:

当前名称:C#中一道关于多线程的基础练习题——模拟仓库存销过程
标题网址:http://www.shufengxianlan.com/qtweb/news21/86571.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联