C#中一道关于线程同步的练习题——模拟多窗口售票

简介: 题目:模拟窗口卖票,四个窗口同时对外开放售票,需要按顺序售出。 要求:输出每一张票的售出时间和售出窗口,不能出现票未售出或者被售出多次的情况。 using System; using System.

题目:模拟窗口卖票,四个窗口同时对外开放售票,需要按顺序售出。

要求:输出每一张票的售出时间和售出窗口,不能出现票未售出或者被售出多次的情况。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.IO;
using System.Reflection;
using System.Diagnostics;

namespace SellTicketsSynchronously
{
    class Program
    {
        //入口
        static void Main(string[] args)
        {
            Ticket tc = new Ticket(10);
            Thread sellWindowA = new Thread(new ParameterizedThreadStart(SellTicket));
            Thread sellWindowB = new Thread(new ParameterizedThreadStart(SellTicket));
            Thread sellWindowC = new Thread(new ParameterizedThreadStart(SellTicket));
            Thread sellWindowD = new Thread(new ParameterizedThreadStart(SellTicket));
            sellWindowA.Name = "Window A";
            sellWindowB.Name = "Window B";
            sellWindowC.Name = "Window C";
            sellWindowD.Name = "Window D";
            sellWindowA.Start(tc);
            sellWindowB.Start(tc);
            sellWindowC.Start(tc);
            sellWindowD.Start(tc);
            sellWindowA.Join();
            sellWindowB.Join();
            sellWindowC.Join();
            sellWindowD.Join();
            Console.WriteLine("Tickets has been sold out. Press any key to quit:");
            Console.ReadLine();
        }
        //卖票方法
        public static void SellTicket(object obj) 
        {
            Ticket ticket = obj as Ticket;
            while (ticket.NumOfTickets>0)
            {
                lock (ticket)
                {
                    if (ticket.NumOfTickets > 0)
                    {
                        try
                        {
                            ticket.NumOfTickets--;
                            Console.WriteLine( DateTime.Now.ToString()+":"+Thread.CurrentThread.Name + " sells a ticket, " + ticket.NumOfTickets + " tickets left.");
                        }
                        catch (Exception ex)
                        {
                            WriteLog(ex);
                        }
                    }
                }
                Random random = new Random();
                Thread.Sleep(random.Next(100,500));
            } 
        }
        //打log方法
        private static void WriteLog(Exception ex)
        {
            string logUrl = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\SellTicketslog.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 Ticket 
    {
        public int NumOfTickets { get; set; }
        public Ticket(int num) 
        {
            this.NumOfTickets = num;
        }
    }
}

运行结果:

不知道这么写会不会有问题,求指点。

————————修改版——————————

经过园友指点,我改用了Task写了这段代码,其间得到了园友的帮助,非常感谢!

修改后的代码如下(蓝色字体为修改的部分):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.IO;
using System.Reflection;
using System.Diagnostics;

namespace SellTicketsSynchronously
{
    class Program
    {
        //入口
        static void Main(string[] args)
        {
            Ticket tc = new Ticket(10);
            WaitForAllSales(tc);
            Console.WriteLine("Tickets has been sold out. Press any key to quit:");
            Console.ReadLine();
        }
        //售罄方法
        private static void WaitForAllSales(Ticket tc) 
        {
            //创建一个Task类型的泛型list
            List<Task> tasks = new List<Task>();
            for (int i = 1; i <= 4; i++)
            {
                //将所有的售票task存入list
                tasks.Add(Task.Run(() => { SellTicket(string.Format("Window"+i), tc); }));
            }
            //等待所有的task都完成
            Task.WaitAll(tasks.ToArray());
        }
        //卖票方法
        public static void SellTicket(string windowName, object obj) 
        {
            string nameOfWindow = windowName;
            Ticket ticket = obj as Ticket;
            while (ticket.NumOfTickets > 0)
            {
                lock (ticket)
                {
                    if (ticket.NumOfTickets > 0)
                    {
                        try
                        {
                            ticket.NumOfTickets--;
                            Console.WriteLine(DateTime.Now.ToString() + ":" + nameOfWindow + " sells a ticket, " + ticket.NumOfTickets + " tickets left.");
                        }
                        catch (Exception ex)
                        {
                            WriteLog(ex);
                        }
                    }
                }
                Random random = new Random();
                Thread.Sleep(random.Next(100,500));
            }
        }
        //打log方法
        private static void WriteLog(Exception ex)
        {
            string logUrl = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\SellTicketslog.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 Ticket 
    {
        public int NumOfTickets { get; set; }
        public Ticket(int num) 
        {
            this.NumOfTickets = num;
        }
    }
}

运行结果:

欢迎大家发散思维,继续提出宝贵意见!:)

 ------------------------------------------------------------------------------------------------------------

经过一位朋友细心的发现,上面这个程序逻辑是有问题的,一直都是售票窗口5在售票,修改后的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.IO;
using System.Reflection;
using System.Diagnostics;

namespace SellTicketsSynchronously
{
    class Program
    {
        //入口
        static void Main(string[] args)
        {
            Ticket tc = new Ticket(20);
            WaitForAllSales(tc);       
            Console.ReadLine();
        }
        //售罄方法
        private static void WaitForAllSales(Ticket tc) 
        {
            //创建一个Task类型的泛型list
            List<Task> tasks = new List<Task>();
            System.Random ran = new Random();
            while (tc.NumOfTickets > 0)
            {
                int i = ran.Next(1,6);
                //将所有的售票task存入list
                tasks.Add(Task.Run(() => { SellTicket(string.Format("Window" + i), tc); }));
                Task.WaitAll(tasks.ToArray());
            }
            Console.WriteLine("Tickets has been sold out. Press any key to quit:");
        }
        //卖票方法
        public static void SellTicket(string windowName, object obj) 
        {
            string nameOfWindow = windowName;
            Ticket ticket = obj as Ticket;
            lock (ticket)
            {
                if (ticket.NumOfTickets > 0)
                {
                    try
                    {
                        ticket.NumOfTickets--;
                        Console.WriteLine(DateTime.Now.ToString() + ":" + nameOfWindow + " sells a ticket, " + ticket.NumOfTickets + " tickets left.");
                    }
                    catch (Exception ex)
                    {
                        WriteLog(ex);
                    }
                }
                Random random = new Random();
                Thread.Sleep(random.Next(100,500));
            }
        }
        //打log方法
        private static void WriteLog(Exception ex)
        {
            string logUrl = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\SellTicketslog.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 Ticket 
    {
        public int NumOfTickets { get; set; }
        public Ticket(int num) 
        {
            this.NumOfTickets = num;
        }
    }
}

本次修改了售罄方法和入口方法(橙色字体),运行结果如下:

欢迎继续提出意见!谢谢大家~

相关文章
|
4月前
|
Web App开发 数据采集 C#
解决Firefox代理身份验证弹出窗口问题:C#和Selenium实战指南
本文是一份实战指南,主要介绍了在使用Selenium和C#进行网页抓取时,如何设置代理服务器的身份验证以避免自动化流程中断。文章首先列出了所需的开发环境和工具,然后通过C#代码示例详细展示了如何在Firefox浏览器中设置代理IP、端口、用户名、密码以及UserAgent和Cookies。代码中包含了自动处理代理身份验证弹出窗口的配置,以及如何添加Cookies的方法。最后,文章强调了结合C#和Selenium可以提高网页抓取任务的稳定性和效率。
解决Firefox代理身份验证弹出窗口问题:C#和Selenium实战指南
|
6月前
|
SQL 开发框架 安全
C#编程与多线程处理
【4月更文挑战第21天】探索C#多线程处理,提升程序性能与响应性。了解C#中的Thread、Task类及Async/Await关键字,掌握线程同步与安全,实践并发计算、网络服务及UI优化。跟随未来发展趋势,利用C#打造高效应用。
196 3
|
6月前
|
安全 编译器 C#
C#学习相关系列之多线程---lock线程锁的用法
C#学习相关系列之多线程---lock线程锁的用法
|
6月前
|
Java 调度 C#
C#学习系列相关之多线程(一)----常用多线程方法总结
C#学习系列相关之多线程(一)----常用多线程方法总结
|
6月前
|
C#
C#学习相关系列之多线程---ConfigureAwait的用法
C#学习相关系列之多线程---ConfigureAwait的用法
112 0
|
6月前
|
C#
C#学习相关系列之多线程---TaskCompletionSource用法(八)
C#学习相关系列之多线程---TaskCompletionSource用法(八)
158 0
|
3月前
|
数据采集 XML JavaScript
C# 中 ScrapySharp 的多线程下载策略
C# 中 ScrapySharp 的多线程下载策略
|
2月前
|
安全 数据库连接 API
C#一分钟浅谈:多线程编程入门
在现代软件开发中,多线程编程对于提升程序响应性和执行效率至关重要。本文从基础概念入手,详细探讨了C#中的多线程技术,包括线程创建、管理及常见问题的解决策略,如线程安全、死锁和资源泄露等,并通过具体示例帮助读者理解和应用这些技巧,适合初学者快速掌握C#多线程编程。
76 0
|
4月前
|
数据采集 Web App开发 JavaScript
快速参考:用C# Selenium实现浏览器窗口缩放的步骤
在C#结合Selenium的网络爬虫应用中,掌握浏览器窗口缩放、代理IP、cookie与user-agent设置至关重要。本文详述了如何配置代理(如亿牛云加强版),自定义用户代理,启动ChromeDriver,并访问目标网站如抖音。通过执行JavaScript代码实现页面缩放至75%,并添加cookie增强匿名性。此策略有效规避反爬机制,提升数据抓取的准确度与范围。代码示例展示了整个流程,确保爬虫操作的灵活性与高效性。
110 3