C#多线程练习题分析
发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,这篇文章主要讲解了"C#多线程练习题分析",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"C#多线程练习题分析"吧!题目:模拟生产、入库、销售(50分)假
千家信息网最后更新 2024年09月22日C#多线程练习题分析
这篇文章主要讲解了"C#多线程练习题分析",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"C#多线程练习题分析"吧!
题目:模拟生产、入库、销售(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; } } }
运行截图:
感谢各位的阅读,以上就是"C#多线程练习题分析"的内容了,经过本文的学习后,相信大家对C#多线程练习题分析这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
仓库
线程
练习题
C#
分析
存货
过程
学习
销售
内容
思路
效果
方法
时候
货物
生产
工整
与此同时
代码
入口
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全特性分别是
c 软件开发笔试题及答案
如何做服务器网站
武术人才数据库王金泉
网络安全法高分答题
网络安全立法历程
广西竺日网络技术有限公司
武汉服务器优惠
龙哥网络技术有限公司
国有网络技术公司
网络安全实用指南视频
安卓开发怎么储存数据库
近年来在网络技术上的成就
网络安全设计环境
服务器iis管理器如何设置
oracle 查询没数据库
服务器 文件 连接
如何设置网络安全课堂
电商平台app软件开发费用
青岸软件开发工作室
q绑在线查询qq数据库
emqx服务器启动不了
域名服务器按区管理的好处
网络技术与应用考题
武汉服务器优惠
游戏软件开发网络公司有哪些
数据库如何处理高并发
数据库安全的关键是什么
数据库怎么分离.mdf文件
网络安全三个基本属性是