Title: Analog production, warehousing, sales (50 points)
Suppose an enterprise self-produced, self-storage, sold, need to plant the various types of products to the warehouse, at the same time, the need to transport the goods in the warehouse to supermarkets and shopping malls for sale, please write a program to simulate this process (mainly 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)
2. When the warehouse is full, it can no longer be stocked in the warehouse. (10 points)
3. When the warehouse is empty, the goods cannot be sold. (10 points)
4. Inventory and pick-up is carried out at the same time, do not appear before the full and then take the goods again full and then take the effect or save one to save again to take such effect. (15 points)
5. Clear thinking, output neat, coding specifications, with the correct exception handling. (10 points)
The process code for storing and selling in a multithreaded simulation warehouse is as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Threading;usingSystem.IO;namespacemultithreadstore{classProgram {//entrance Static voidMain (string[] args) {Goods Goods=NewGoods (); Thread Storegoods=NewThread (NewParameterizedthreadstart (store)); Thread Sellgoods=NewThread (NewParameterizedthreadstart (Sell)); Storegoods.start (goods); Sellgoods.start (goods); Console.ReadLine (); } //Inventory Method Private Static voidStoreObjectobj) { BOOLStoreflag =true; Random Random=NewRandom (); while(storeflag) {Try{Goods Goods= obj asGoods; 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 ( -, +)); } Catch(Exception ex) {Writelog (ex); Storeflag=false; } } } //method of selling goods Public Static voidSellObjectobj) { BOOLSellflag =true; Random Random=NewRandom (); while(sellflag) {Try{Goods Goods= obj asGoods; if(Goods. Num >0) {goods. Num--; Console.WriteLine ("Sell a goods,"+ Goods. Num +"Goods left!"); } Else{Console.WriteLine ("There is no goods now."); } thread.sleep (random. Next ( +,4000)); } Catch(Exception ex) {Writelog (ex); Sellflag=false; } } } //hit Log Method Private Static voidWritelog (Exception ex) {stringLogURL = Environment.getfolderpath (Environment.SpecialFolder.DesktopDirectory) +"\\MuliThreadStorelog.txt"; if(File.exists (@logUrl)) {using(FileStream fs =NewFileStream (LogURL, Filemode.append)) { using(StreamWriter SW =NewStreamWriter (FS, Encoding.default)) { Try{SW. Write (ex); } Catch(Exception ex1) {writelog (EX1); } finally{SW. Close (); Fs. Close (); } } } } Else { using(FileStream fs =NewFileStream (LogURL, FileMode.CreateNew)) { using(StreamWriter SW =NewStreamWriter (FS, Encoding.default)) { Try{SW. Write (ex); } Catch(Exception ex1) {writelog (EX1); } finally{SW. Close (); Fs. Close (); } } } } } } //Goods category classGoods { Public intNum {Get;Set; } Public intMaxnum {Get;Set; } PublicGoods () {Num=Ten; Maxnum= -; } }}
Run:
A basic exercise on multithreading in C #--simulating the storage and selling process of a warehouse