In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "analysis of C# multithreaded exercises". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "C# multithreaded exercise analysis".
Topic: simulated production, storage, sales (50 points)
Suppose an enterprise produces, stores and sells itself, and needs to transport all kinds of products produced by the factory to the warehouse indefinitely, and at the same time, the goods in the warehouse need to be shipped to supermarkets and shopping malls for sale. please write a program to simulate this process (mainly to access this process).
Scoring criteria:
1. The stock of the warehouse is fixed and can be assumed to be a constant, such as 10. (5 points)
two。 When the warehouse is full, you can no longer stock it in the warehouse. (10 points)
3. The goods cannot be sold when the warehouse is empty. (10 points)
4. Inventory and pick-up are carried out at the same time, do not have the effect of first full and then full and then full or deposit one by one and then withdraw again. (15 points)
5. The train of thought is clear, the output is neat, the coding standard, has the correct exception handling. (10 points)
The process code for simulating warehouse storage and sales with multithreading is as follows:
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 {/ / entry 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 ();} / / inventory method 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 } / / typing the log method 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 ();}} / / goods category class Goods {public int Num {get; set;} public int MaxNum {get; set } public Goods () {Num = 10; MaxNum = 50;}
Run the screenshot:
Thank you for your reading, the above is the content of "C# multithreaded exercise analysis". After the study of this article, I believe you have a deeper understanding of the problem of C# multithreaded exercise analysis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.