• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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

评分标准:

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

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

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

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

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

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

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;
                }
            }
        }
        //卖货方法
        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();
                        }
                    }
                }
            }
        }
    }
    //货品类
    class Goods 
    {
        public int Num { get; set; }
        public int MaxNum { get; set; }
        public Goods() 
        {
            Num = 10;
            MaxNum = 50;
        }     
    }
}

运行截图:

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#winform打开主界面并关闭登录界面发布时间:2022-07-13
下一篇:
C/C++笔试题整理发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap