日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
C#中一道關于多線程的基礎練習題——模擬倉庫存銷過程

題目:模擬生產、入庫、銷售(50分)

創(chuàng)新互聯(lián)公司是一家集成都網站設計、成都做網站、外貿網站建設、網站頁面設計、網站優(yōu)化SEO優(yōu)化為一體的專業(yè)網站制作公司,已為成都等多地近百家企業(yè)提供網站建設服務。追求良好的瀏覽體驗,以探求精品塑造與理念升華,設計最適合用戶的網站頁面。 合作只是第一步,服務才是根本,我們始終堅持講誠信,負責任的原則,為您進行細心、貼心、認真的服務,與眾多客戶在蓬勃發(fā)展的市場環(huán)境中,互促共生。

 假設某企業(yè)自產、自存、自銷,需要將工廠生產的各類產品不定時的運到倉庫,與此同時,需要將倉庫中的貨物運往超市和商場中進行銷售,請編寫一個程序模擬此過程(主要是存取這個過程)。

評分標準:

1. 倉庫的存量是固定的,可以假設為一個常量,比如10。(5分)

2. 倉庫滿的時候,不能再向倉庫中存貨。(10分)

3. 倉庫空的時候,不能賣出貨物。(10分)

4. 存貨和取貨是同時進行的,不要出現(xiàn)先存滿再取完貨再存滿再取完的效果或者存一個取一個再存再取這樣的效果。(15分)

5. 思路清晰,輸出工整,編碼規(guī)范,有正確的異常處理。(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.dlmjj.cn/article/dhghjhi.html